根据另一列中的值,用字符串替换一列中的NaN

时间:2014-02-21 19:39:13

标签: python pandas

简单地说,在列B ='t3'的情况下,我想用新字符串替换A列中的NaN值。

我在下面的尝试都失败了。

d = pd.DataFrame({"A":[np.nan, 't2', np.nan, 't3', np.nan], "B":['t1', 't2', 't3', 't4', 't3']})
print "Original Dataframe:\n", d

# Does not work
d[d.B == 't3'].A = 'new_val'

# Does not work
d[d.B == 't3'].A.replace(np.nan, 'new_val')


# Does not work
d[d.B == 't3'].A.replace(np.nan, 'new_val', inplace=True)

print "Final Dataframe:\n", d

这是输出:

Original Dataframe:
     A   B
0  NaN  t1
1   t2  t2
2  NaN  t3
3   t3  t4
4  NaN  t3

[5 rows x 2 columns]
Final Dataframe:
     A   B
0  NaN  t1
1   t2  t2
2  NaN  t3
3   t3  t4
4  NaN  t3

1 个答案:

答案 0 :(得分:5)

使用loc,请参阅http://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing-loc-iloc-and-ix

In [5]:

d.loc[(d['A'].isnull()) & (d.B == 't3'), 'A']='new_val'

d

Out[5]:

         A   B
0      NaN  t1
1       t2  t2
2  new_val  t3
3       t3  t4
4  new_val  t3

[5 rows x 2 columns]