Create new column based on condition on other categorical column

我只是一个虾纸丫 提交于 2021-02-07 23:57:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!