在数据框中查找通讯者进行计算

如下两个数据框,我想计算相关系数。


当两列都用实际值完成时,它工作正常。但如果不是,则在计算相关系数时取零作为值。


例如,Addison 和 Caden 的权重为 0。Jack 和 Noah 没有权重。我想排除它们进行计算。


(在尝试中,似乎只考虑相同的长度,即自动排除 Jack 和 Noah – 是吗?)


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


谢谢你。


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)


米琪卡哇伊
浏览 168回答 2
2回答

翻阅古今

屏蔽和取非零值和公共索引: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, :].indexdf_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'])

MMMHUHU

屏蔽和取非零值和公共索引: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, :].indexdf_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'])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python