我一生无法解决这个问题。
我有两个数据框,一个包含100多个列和54k个案例,另一个数据框是汇总来自完整数据集的信息的数据框,该数据框由名为“ countyName”的列(具有66个唯一值)分组。
我尝试创建的功能会测试按“ countyName”分组的列,如果该列县的所有值均为-777,则返回-777,否则,返回原始值。
这是代码:
def change777s(report, df):
list777 = list(df.columns[(df == -777).any()])
report[list777]=df.groupby('countyName')[list777].agg(lambda x: -777 if x.all()==-777 else x)
return report
df2_first=change777s(df2_first, first)
但我不断得到:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
或完全不正确的结果。
感谢您的帮助。