代码之家  ›  专栏  ›  技术社区  ›  Mark K

在数据帧中查找对应项以进行计算

  •  1
  • Mark K  · 技术社区  · 6 年前

    下面两个数据帧,我想计算相关系数。

    当两列都用实际值完成时,它可以正常工作。但当它们不存在时,在计算相关系数时以零为值。

    例如,Addison_ s和Caden_ s的权重为0。杰克和诺亚没有体重。我想把它们排除在外进行计算。

    (在尝试中,似乎只考虑相同的长度,即杰克和诺亚被自动排除在外吗?)

    我如何才能只包括非零值的人进行计算?

    谢谢您。

    import pandas as pd
    
    Weight = {'Name': ["Abigail","Addison","Aiden","Amelia","Aria","Ava","Caden","Charlotte","Chloe","Elijah"], 
    'Weight': [10, 0, 12, 20, 25, 10, 0, 18, 16, 13]}
    
    df_wt = pd.DataFrame(Weight)
    
    Score = {'Name': ["Abigail","Addison","Aiden","Amelia","Aria","Ava","Caden","Charlotte","Chloe","Elijah", "Jack", "Noah"], 
    'Score': [360, 476, 345, 601, 604, 313, 539, 531, 507, 473, 450, 470]}
    
    df_sc = pd.DataFrame(Score)
    
    print df_wt.Weight.corr(df_sc.Score)
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Space Impact    6 年前

    掩蔽并取非零值和共同指数:

    df_wt.set_index('Name', inplace=True)
    df_sc.set_index('Name', inplace=True)
    
    mask = df_wt['Weight'].ne(0)
    common_index = df_wt.loc[mask, :].index
    df_wt.loc[common_index, 'Weight'].corr(df_sc.loc[common_index, 'Score'])
    
    0.923425144491911
    

    如果两个数据帧都包含零,则:

    mask1 = df_wt['Weight'].ne(0)
    mask2 = df_sc['Score'].ne(0)
    common_index = df_wt.loc[mask1, :].index.intersection(df_sc.loc[mask2, :].index)
    df_wt.loc[common_index, 'Weight'].corr(df_sc.loc[common_index, 'Score'])
    
        2
  •  1
  •   jezrael    6 年前

    使用 map 对于“添加新列”,请删除 0 按行排列 boolean indexing 最后在同一个数据帧中应用您的解决方案:

    df_wt['Score'] = df_wt['Name'].map(df_sc.set_index('Name')['Score'])
    
    df_wt = df_wt[df_wt['Weight'].ne(0)]
    print (df_wt)
            Name  Weight  Score
    0    Abigail      10    360
    2      Aiden      12    345
    3     Amelia      20    601
    4       Aria      25    604
    5        Ava      10    313
    7  Charlotte      18    531
    8      Chloe      16    507
    9     Elijah      13    473
    
    print (df_wt.Weight.corr(df_wt.Score))
    0.923425144491911