即使数据中没有字符串,Pandas corrwith 也会抛出 str 对象错误

时间:2021-03-22 15:22:24

标签: python pandas error-handling correlation

出于某种原因,当我对我的数据调用 corrwith() 时,我收到一条错误消息,指出我的数据中有字符串,即使没有。我已经对 type() 进行了几次不同的测试来确认这一点。

 df = pd.DataFrame(
 {'continuous_1': {156: 1495.6562554178136, 157: 9589.977639938277, 159: 8414.10911057493,
  160: 24.56652864929212, 161: 13.556781710107797},
 'continuous_2': {156: 7.310320356341689, 157: 9.168473836272918, 159: 9.037665231807642,
  160: 3.2013848924364523, 161: 2.606886917330664},
 'continuous_3': {156: 0.0, 157: 2.8, 159: 2.8, 160: 0.0, 161: 0.0},
 'bool_1': {156: 0, 157: 0, 159: 0, 160: 1, 161: 1},
 'continuous_1': {156: 1.1566869, 157: 2.6281624, 159: 2.6281624,
  160: 1.3115032,
  161: 2.8945127},
 'continuous_2': {156: 1.2319942712783811,
  157: 2.371660312016805,
  159: 2.1829558610916138,
  160: 1.5209104617436726,
  161: 2.2596973180770874},
 'categorical_1': {156: 0, 157: 0, 159: 0, 160: 0, 161: 0}}
 )

 df.corrwith('categorical_1')

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-85-801e8ac90c04> in <module>
     13   160: 1.5209104617436726,
     14   161: 2.2596973180770874}}
---> 15             ).corrwith('categorical_1')

~/.local/lib/python3.7/site-packages/pandas/core/frame.py in corrwith(self, other, axis, drop, method)
   8574             return this.apply(lambda x: other.corr(x, method=method), axis=axis)
   8575 
-> 8576         other = other._get_numeric_data()
   8577         left, right = this.align(other, join="inner", copy=False)
   8578 

AttributeError: 'str' object has no attribute '_get_numeric_data'

1 个答案:

答案 0 :(得分:1)

corrwith 将系列作为参数而不是列名

使用

df.corrwith(df.categorical_1)