【发布时间】:2019-01-23 18:13:05
【问题描述】:
我有一个 CSV 文件,其中“性别”列中缺少一些字段。所以我需要使用 python 的 fillna() 函数自动填充它们。我给出的条件是,如果申请人收入大于 20000,则必须使用“性别”列中的“男性”标签更新字段。相同的代码粘贴在下面,以及与该错误相关的错误。所以,谁能帮我解决这个错误
如果数据['ApplicantIncome'] >= 20000: data['Gender'].fillna(data['Gender'] == 'Male',inplace=True)
错误如下:
ValueError Traceback (most recent call last)
<ipython-input-97-19fee6c4a819> in <module>
----> 1 if data['ApplicantIncome'] >= 20000:
2 data['Gender'].fillna(data['Gender'] == 'Male',inplace=True)
~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1574 raise ValueError("The truth value of a {0} is ambiguous. "
1575 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1576 .format(self.__class__.__name__))
1577
1578 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
【问题讨论】:
-
@PatrickArtner - 你说的完全正确。但是根据我的数据集,当我按性别绘制箱线图时,所有收入超过 20000 的人都是男性。所以这就是我使用它作为参数的唯一原因。
标签: python python-3.x pandas