我有一个如下所示的数据框
unsigned
我需要根据以下条件创建一个Flag列Flag
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
答案 0 :(得分:3)
第一个链条件由&
表示按位AND
,然后由|
表示按位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
或将True/False
映射到1/0
:
df['Flag'] = (m1 | m2 | m3).astype(int)