【发布时间】:2020-05-04 01:25:21
【问题描述】:
我有一个需要按id 分组的数据框,然后在这些组中,比较列A 和B 以更改列C。
这是我的代码:
def alter_C(row):
if row['A'].shift(1) == row['B'] :
row['C'] = 'new_value'
return row
df = df.groupby(id).apply(alter_C)
我得到错误:
ValueError:Series 的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()。
你能帮忙吗?
【问题讨论】:
-
这里不需要groupby,检查np.where
-
编辑了我的帖子,我需要一个分组依据,因为我正在比较这些组中的连续行,感谢您的回复
标签: python pandas pandas-groupby