【发布时间】:2021-12-28 17:38:11
【问题描述】:
我有一个 Pandas 数据框,其中有一列名为 col1,我想在数据框中添加一个新列,该列的值取决于每条记录的 col1 的内容。我试过这个:
d['new_column'] = 'CASE 1' if d['COL1'] == None else 'CASE 2' if d['COL1'] == 0 else 'CASE 3'
但是我收到了这个错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
如何进行评估以正确设置新列的值?
【问题讨论】:
标签: python-3.x pandas dataframe