亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在數據框中查找通訊者進行計算

在數據框中查找通訊者進行計算

米琪卡哇伊 2021-10-12 15:02:04
如下兩個數據框,我想計算相關系數。當兩列都用實際值完成時,它工作正常。但如果不是,則在計算相關系數時取零作為值。例如,Addison 和 Caden 的權重為 0。Jack 和 Noah 沒有權重。我想排除它們進行計算。(在嘗試中,似乎只考慮相同的長度,即自動排除 Jack 和 Noah – 是嗎?)如何只包括非零值的人進行計算?謝謝你。import pandas as pdWeight = {'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 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

屏蔽和取非零值和公共索引:


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'])


查看完整回答
反對 回復 2021-10-12
?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

屏蔽和取非零值和公共索引:


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'])


查看完整回答
反對 回復 2021-10-12
  • 2 回答
  • 0 關注
  • 201 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號