2

我有类似如下的数据:

   A    B    C
0  M    M    M
1  Y    M    M
2  Y  NaN  NaN
3  Y    Y  etc

我需要的是:

   A    B    C  F
0  M    M    M  3
1  Y    M    M  4
2  Y  NaN  NaN  0
3  Y    Y  etc  5

我不知道如何处理rows[2, 3],这里我列出了我使用但不起作用的代码:

df.loc[df['A'] == 'M', 'F'] = '3'
df.loc[((df.A != 'M') & (df.B == 'M')), 'F'] = '4'
df.loc[(df.A != 'M') & (df.B != 'M') & (df.C != ''), 'F'] = '5'
df.loc[(df.A != 'M') & (df.B != 'M') & (df.C == ''), 'F'] = '0'
4

1 回答 1

3

numpy.select与 一起使用Series.notna

m1 = df['A'] == 'M'
m2 = df['B'] == 'M'
m3 = df['C'].notna()

df['F'] = np.select([m1, m2, m3], ['3','4','5'], default='0')
print (df)
   A    B    C  F
0  M    M    M  3
1  Y    M    M  4
2  Y  NaN  NaN  0
3  Y    Y  etc  5

如有必要,通过-添加更多~用于反转掩码和链的条件:bitwise AND&

m1 = df['A'] == 'M'
m2 = df['B'] == 'M'
m3 = df['C'].notna()
m11 = ~m1
m22 = ~m2
m33 = ~m3

df['F'] = np.select([m1, m2 & m11, m3 & m11 & m22], ['3','4','5'], default='0')

编辑:

您的解决方案可以通过Series.isnaand 改变Series.notna

df.loc[df['A'] == 'M', 'F'] = '3'
df.loc[((df.A != 'M') & (df.B == 'M')), 'F'] = '4'
df.loc[(df.A != 'M') & (df.B != 'M') & (df.C.notna()), 'F'] = '5' 
df.loc[(df.A != 'M') & (df.B != 'M') & (df.C.isna()), 'F'] = '0'
于 2019-04-23T11:54:32.930 回答