簡體   English   中英

在 python 中創建 Pearson 相關系數的問題

[英]Problem with creating Pearson correlation coefficient in python

問題:根據第三列的值創建 Pearson 相關系數。

首先,我有一個 3 列的 dataframe。 A、B 和 C

Col. A 和 B 包含 float64 類型,而在 C 中有對象。 我想獲得 col A 和 B 的 Pearson 相關系數。

print(df['A'].corr(df['B'],method='pearson')) --> 這適用於整個列。

在下一步我掙扎。 C 列只有 2 個值。 我們稱它們為 c1 和 c2。 我現在想獲得 c1 和 c2 的系數。 我試過了

print(df['A']&df['C']=='c1').corr((df['B']&df['C']=='c1'),method='pearson')

和 c2 一樣。 記錄的錯誤是:TypeError: unsupported operand type(s) for &: 'float' and 'str' How can I get both coefficients without split the dataframe?

提前致謝

這應該可以實現您正在尋找的東西:

print(df[df['C']=='c1']['A'].corr(df[df['C']=='c1']['B'],method='pearson'))

df[df['C']=='c1']檢索 dataframe 的子集,其中 C 列中的值為“c1”,然后您只需照常調用您想要的列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM