如何 select NaN 行并将 value_A 分配给新列?代码 df.loc['condition_1' & 'condition_2' & 'df.column_a == ''] = 'value_a'
how to select NaN rows and assign value_A to a new column? code df.loc['condition_1' & 'condition_2' & 'df.column_a == ''] = 'value_a'
我有类似如下的数据:
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
我不知道如何处理行[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'
使用numpy.select
with 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.isna
和
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'
我有类似如下的数据:
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
我不知道如何处理行[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'
使用numpy.select
with 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.isna
和
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'