首页 > 解决方案 > 在 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().

标签: pythonpandas

解决方案


推荐阅读