大Pandas的条件反射

2024-04-26 23:23:39 发布

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

我正在尝试获取一个pandas dataframe,并在添加新列“Size_Category”后返回一个pandas dataframe对象,该列的值根据某些条件为small-medium或large。在

mod_df = df.copy(deep=True)
mod_df.loc[(mod_df['Length'] <= 300 , 'Size_Category')] = 'small' # condition, new_column
mod_df.loc[(mod_df['Length'] <= 300 | mod_df['Length'] > 450) , 'Size_Category')] = 'medium' # condition, new_column
mod_df.loc[(mod_df['Length'] >= 450, 'Size_Category')] = 'large' # condition, new_column

当我这样做的时候,它会给我一个错误的说法

序列的真值是模棱两可的。使用a.empty、a.bool()、a.item()、a.any()或a.all()。在

我该怎么办?在


Tags: 对象moddataframepandasdfnewsizecolumn