【发布时间】:2022-08-13 13:57:04
【问题描述】:
我的目标是根据 A 到 C 列将值放入 D 列。如果值大于 20,我想遍历 A、B 和 C 列中的每一列,并在 D 列中加 1。
import pandas as pd
data={\'A\':[5,2,25,4],\"B\":[15,22,100,24], \"C\":[4, 100, 0, 19], \"D\" : [0,0,0,0]}
df= pd.DataFrame(data)
for x in range(0, len(df.columns)):
if df.iloc[:, x] > 20:
df[\"D\"] = df[\"D\"] +1
else:
df[\"D\"] = df[\"D\"]
df
因此,根据示例代码,D 列应如下所示:
| D |
|---|
| 0 |
| 2 |
| 2 |
| 1 |
但是,我不断收到此错误-ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。 我在这里查看了其他答案 (Truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()),这表明这是一个多运算符问题,但我没有在这里使用这些。
我究竟做错了什么?