python - 在 pandas 数据帧上使用 iterrows 和逻辑脚本的问题
问题描述
我正在尝试确定“日期”(为此已转换为整数)何时大于 30,如果是,则该个人是否还有另一个日期的频率大于 1(至少 2)如果这些是真的,我想将“顶部”日期(出现次数最多的日期)放入一个新列中,但我遇到了一个错误。
for index_label, row_series in describe_crt.iterrows():
if (describe_crt["delta_days_post_crt"] != 0) & (describe_crt["delta_days_post_crt"] > 30) & (describe_crt["freq"] > 1):
describe_crt.at[index_label , "best_crt_date"] = describe_crt["top"]```
> ValueError Traceback (most recent call
> last) <ipython-input-108-9cc36c985dca> in <module>
> 7 # iterate over the dataframe row by row
> 8 for index_label, row_series in describe_crt.iterrows():
> ----> 9 if (describe_crt["delta_days_post_crt"] != 0) & (describe_crt["delta_days_post_crt"] > 30) & (describe_crt["freq"] >
> 1):
> 10 describe_crt.at[index_label , "best_crt_date"] = describe_crt["top"]
> 11
>
> ~/opt/anaconda3/lib/python3.7/site-packages/pandas/core/generic.py in
> __nonzero__(self) 1553 "The truth value of a {0} is ambiguous. " 1554 "Use a.empty, a.bool(), a.item(),
> a.any() or a.all().".format(
> -> 1555 self.__class__.__name__ 1556 ) 1557 )
>
> ValueError: The truth value of a Series is ambiguous. Use a.empty,
> a.bool(), a.item(), a.any() or a.all().
解决方案
推荐阅读
- json - 使用 Circe 解码对象序列
- vb.net - 如何关闭使用窗口句柄嵌入的嵌入窗口?
- r - 如何获得 3 个数据帧的 z 分数分布并在一张图中全部绘制
- vala - 重用信号处理程序?
- php - simpleSAML 远程被动登录
- r - 对数据框中的所有变量执行相同的变异
- python - Python order 具有预定义顺序的字典
- angular - 如何使用来自其他 Angular 组件的`templateref`?
- node.js - 使用 node.js 使用 jira-connector 库从 jira 获取 json 数据在 heroku 中部署时不起作用
- asp.net-core - 手动触发 IOptionsMonitor<>.OnChange