基于另一列(float)更改列(分类)值

2024-04-27 20:53:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试执行此代码来替换所有的df\u dash['aoh']<=262转待命

if df_dash['aoh'] <= 262:
   df_dash['category'] = 'standby'

然而,我得到这个错误,我似乎找不到一个方法来获得正确的结果

Traceback (most recent call last): File "<input>", line 1, in <module> File "/Users/jacob/anaconda3/envs/MERS/lib/python3.7/site-packages/pandas/core/generic.py", line 1555, in __nonzero__ self.__class__.__name__ ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

我在其他线程中读到过这个错误,但是我似乎找不到解决这个特定问题的方法

我也试过用这个

df_dash['category'] = np.where(df_dash['aoh'] <= 262, 'standby', df['category'])

但是,我明白了

ValueError: operands could not be broadcast together with shapes (458,) () (4173,) 

希望有人能帮忙


Tags: 方法代码inltdfif错误line
3条回答

一文不值-您的np.where解决方案本应有效,但我认为您在函数调用的第二部分使用了df['category'],而不是df_dash['category']

看不到其余的代码,但假设数据帧的大小不同

 df_dash['category'][df_dash['aoh'] <= 262] = 'standby'

可以将locboolean indexing一起使用:

df_dash.loc[df_dash['aoh'] <= 262,'category'] = 'standby'

相关问题 更多 >