填充有条件的列

时间:2017-06-27 08:41:11

标签: python pandas numpy dataframe

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

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

我想用条件填写NaN:

BufferedImage

有什么建议吗? (不知道是否可能)

非常感谢!

3 个答案:

答案 0 :(得分:3)

对于条件maskisnull,我认为您需要isin

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

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

另一个条件和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

答案 1 :(得分:0)

此处.loc解决方案

df.loc[df.Col1.isnull() & df.Col2.isin(['Someval1']), 'Col1'] = 'Y'
df.loc[df.Col1.isnull() & df.Col2.isin(['Someval4']), 'Col1'] = 'N'
df.loc[df.Col1.isnull() & df.Col2.isin([np.nan]), :] = 'N'

完整的脚本:

df = pd.read_csv(StringIO("""Col1          Col2
1          NaN         Someval1
2           Y          Someval2
3           N          Someval3
4          NaN           NaN
5          NaN         Someval4
"""), sep="\s+")

df.loc[df.Col1.isnull() & df.Col2.isin(['Someval1']), 'Col1'] = 'Y'
df.loc[df.Col1.isnull() & df.Col2.isin(['Someval4']), 'Col1'] = 'N'
df.loc[df.Col1.isnull() & df.Col2.isin([np.nan]), :] = 'N'

df

  Col1      Col2
1    Y  Someval1
2    Y  Someval2
3    N  Someval3
4    N         N
5    N  Someval4

答案 2 :(得分:0)

您可以使用

df.Col1[(df['Col1'].isnull())&(df['Col2']=='Someval1')] = 'Y'
df.Col1[(df['Col1'].isnull())&(df['Col2']=='Someval4')] = 'N'
df.Col1[(df['Col1'].isnull())&(df['Col2'].isnull())] = 'N'