按条件填充列

dat*_*ana 4 python numpy dataframe pandas

我有一个包含 2 列的数据框:

          Col1          Col2
1          NaN         Someval1
2           Y          Someval2
3           N          Someval3
4          NaN           NaN
5          NaN         Someval4
Run Code Online (Sandbox Code Playgroud)

我想用以下条件填充 NaN:

If Col1 has NaN and Col2 has a Someval1 that is in list 1 then fillna with Y
If Col1 has NaN and Col2 has a Someval4 that is in list 2 then fillna with N
If Col1 has NaN and Col2 has a NaN that is in list 2 then fillna with N
Run Code Online (Sandbox Code Playgroud)

有什么建议 ?(不知道可不可以)

非常感谢 !

jez*_*ael 7

我认为你需要mask,条件isnullisin

L1 = ['Someval1','Someval8']
L2 = ['Someval4','Someval9', np.nan]
m1 = df['Col1'].isnull()
m2 = df['Col2'].isin(L1)
m3 = df['Col2'].isin(L2)

df['Col1'] = df['Col1'].mask(m1 & m2, 'Y')
df['Col1'] = df['Col1'].mask(m1 & m3, 'N')

print (df)
  Col1      Col2
1    Y  Someval1
2    Y  Someval2
3    N  Someval3
4    N       NaN
5    N  Someval4
Run Code Online (Sandbox Code Playgroud)

另一个解决方案numpy.where

df['Col1'] = np.where(m1 & m2, 'Y',
             np.where(m1 & m3, 'N', df['Col1']))

print (df)
  Col1      Col2
1    Y  Someval1
2    Y  Someval2
3    N  Someval3
4    N       NaN
5    N  Someval4
Run Code Online (Sandbox Code Playgroud)

另一种解决方案具有一个条件 和fillna

L1 = ['Someval1','Someval8']
L2 = ['Someval4','Someval9', np.nan]

df['Col1'] = df['Col1'].mask(df['Col2'].isin(L1), df['Col1'].fillna('Y'))
df['Col1'] = df['Col1'].mask(df['Col2'].isin(L2), df['Col1'].fillna('N'))
print (df)
  Col1      Col2
1    Y  Someval1
2    Y  Someval2
3    N  Someval3
4    N       NaN
5    N  Someval4
Run Code Online (Sandbox Code Playgroud)