1

考虑以下数据集(作为 pandas df 导入):

Chinese Yuan    Euro    Japanese Yen    U.K. Pound Sterling U.S. Dollar Indian Rupee
9.29446 1.25367 143.645 1.05054 1.39596 94.1451
9.31515 1.25322 142.715 1.06143 1.39684 94.1513
9.31697 1.25834 140.54  1.073   1.39286 NA
9.31315 1.25737 140.64  1.06911 1.39316 94.0345
9.3135  1.25797 140.51  1.07261 1.39257 93.9408
9.29403 1.25769 139.962 1.0705  1.38962 93.3027
9.31021 1.2549  143.369 1.05762 1.39194 93.4641
9.3135  1.25716 145.178 1.0468  1.39193 93.5445
9.30432 1.24695 144.895 1.05236 1.39122 93.0917
9.31532 1.25263 147.268 1.04242 1.39392 NA
9.30354 1.25652 NA  1.04952 1.38883 93.1923

我想做的是找出Indian Rupee所有其他货币的相关性,为此,我正在尝试使用以下python代码:

df['Indian Rupee'].corr(~df['Indian Rupee'])

上面抛出了一个错误:

TypeError: ufunc 'invert' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

我想了解为什么会出现上述错误?为什么我不能以这种方式找到相关性?

我在这里有什么选择?

4

2 回答 2

3

~是按位取反运算符。您可以否定布尔值(系列)或整数(系列)。您不能否定不安全的浮动系列。

换句话说~df['Indian Rupee'],不会为您排除其他列。如果要删除,请使用:

df.drop('Indian Rupee', axis=1)

所以你可以做

df.drop('Indian Rupee', axis=1).corrwith(df['Indian Rupee'])

输出:

Chinese Yuan           0.267802
Japanese Yen          -0.270123
U.K. Pound Sterling    0.197496
U.S. Dollar            0.846584
dtype: float64
于 2021-03-12T19:14:36.597 回答
3

一种选择是对整个数据框进行关联,然后只需选择您关心的列

df.corr()['Indian Rupee']

ChineseYuan          0.304050
Euro                 0.243851
JapaneseYen         -0.270123
U.K.PoundSterling    0.314681
U.S.Dollar           0.872862
IndianRupee          1.000000
Name: IndianRupee, dtype: float64
于 2021-03-12T19:12:17.823 回答