gpt4 book ai didi

python - 如何选择 NaN 行并将 value_A 分配给新列?代码 df.loc ['condition_1' & 'condition_2' & 'df.column_a == ' '] = 'value_a'

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:51 25 4
gpt4 key购买 nike

我有类似如下的数据:

   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'

最佳答案

使用numpy.selectSeries.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

如有必要,添加更多条件使用~ 通过按位与 - & 反转掩码和链:

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'

关于python - 如何选择 NaN 行并将 value_A 分配给新列?代码 df.loc ['condition_1' & 'condition_2' & 'df.column_a == ' '] = 'value_a',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55810607/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com