我有一个如下所示的数据框
Category Value
A 10
B 22
A 2
C 30
B 23
B 4
C 8
C 24
A 9
我需要创建一个标志列,基于以下条件:
If the values of Category A is greater than or equal 5 then Flag=1, else 0
If the values of Category B is greater than or equal 20 then Flag=1, else 0
If the values of Category C is greater than or equal 25 then Flag=1, else 0
期望的输出如下所示。
Category Value Flag
A 10 1
B 22 1
A 2 0
C 30 1
B 23 1
B 4 0
C 8 0
C 24 0
A 9 1
我尝试了以下代码。
df_['Flag'] = np.where(df_[df_['Category']=='A']['Value']>=5, 1, 0)
df_['Flag'] = np.where(df_[df_['Category']=='B']['Value']>=20, 1, 0)
df_['Flag'] = np.where(df_[df_['Category']=='C']['Value']>=25, 1, 0)