Python If Else 分段 'Invalid Type Comparison' Dataframe 错误

Python If Else Segmentation 'Invalid Type Comparison' Error in Dataframe

我有一个示例数据框 (df_merged_1),如下所示:

Revenue 列是 float64 dtype。我想创建一个名为 'Revenue_Segment' 的新列。这就是我想要的最终结果:

下面是我用来分段的代码:

if df_merged_1['Revenue'] >= 0 and df_merged_1['Revenue'] <= 2200:
    df_merged_1['AUM_Segment'] == 'test1'
else:
    df_merged_1['AUM_Segment'] == 'test0'

但是代码不起作用...我收到以下错误:

TypeError: invalid type comparison

非常感谢任何帮助!

不优雅,但这是我现在能想到的唯一解决方案:

# Make a new list to add it later to your df
rev_segment = []

# Loop through the Revenue column
for revenue in df_merged_1['Revenue']:
    if revenue >= 0 and revenue <= 2200:
        rev_segment.append('test1')
    else:
        rev_segment.append('test0')

# Now append the list to a new column
df_merged_1['Revenue_Segment'] = rev_segment 

试试这个:

df_merged_1['AUM_Segment'] =  \
    np.where((df_merged_1['Revenue']>=0) & (df_merged_1['Revenue']<=2200), 'test1', 'test2')