python - 即使数据中没有字符串,Pandas corrwith 也会引发 str 对象错误
问题描述
出于某种原因,当我调用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'
解决方案
corrwith
将系列作为参数而不是列名
利用
df.corrwith(df.categorical_1)
推荐阅读
- javascript - 在 Javascript 端保存处理程序之前计算 vTiger 字段(内部代码)
- reactjs - 过多的项目渲染
- javascript - 角度材质 - 对材质表进行排序后,动画过渡开始状态未更新
- unity3d - 在 Unity 的多选下拉菜单中显示 ScriptableObjects
- reactjs - 传递参数 onClick, React
- android - 唯一约束失败:type.ps
- yii2 - Yii2 使用 ajax 提交模态表单
- java - 在 jooq 3.11.11 试用版的 META-INF/maven/plugin.xml 中找不到插件描述符
- android - 跳尺的制作方法
- android - 如何在 Android 上显示来自 Base64 字符串的 PDF?