问题
I have a dataframe as shown below
Category Value
A 10
B 22
A 2
C 30
B 23
B 4
C 8
C 24
A 9
I need to create a Flag column Flag based following conditions
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
Expected output as shown below
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
I tried following code
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)
回答1:
First chain condition by &
for bitwise AND
and then by |
for bitwise OR
:
m1 = (df['Category']=='A') & (df['Value']>=5)
m2 = (df['Category']=='B') & (df['Value']>=20)
m3 = (df['Category']=='C') & (df['Value']>=25)
df['Flag'] = np.where(m1 | m2 | m3, 1, 0)
print (df)
Category Value Flag
0 A 10 1
1 B 22 1
2 A 2 0
3 C 30 1
4 B 23 1
5 B 4 0
6 C 8 0
7 C 24 0
8 A 9 1
Or map True/False
to 1/0
:
df['Flag'] = (m1 | m2 | m3).astype(int)
来源:https://stackoverflow.com/questions/57669111/create-new-column-based-on-condition-on-other-categorical-column