我有使用 Pandas DataFrame 表示的数据,例如如下所示:
| id | entity | name | value | location
其中id是一个integer值、entity是一个integer、name是一个string、value是一个integer、和location是一个string(例如美国、加拿大、英国等)。
现在,我想向此数据框中添加一个新列,即列“ flag”,其中的值分配如下:
for d in df.iterrows():
if d.entity == 10 and d.value != 1000 and d.location == CA:
d.flag = "A"
elif d.entity != 10 and d.entity != 0 and d.value == 1000 and d.location == US:
d.flag = "C"
elif d.entity == 0 and d.value == 1000 and d.location == US"
d.flag = "B"
else:
print("Different case")
有没有办法加快速度并使用一些内置函数而不是 for 循环?
郎朗坤
LEATH
绝地无双
相关分类