简单地说,在列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
答案 0 :(得分:5)
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]