简单地说,在 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
最佳答案
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]
关于python - 根据另一列中的值,用字符串替换一列中的 NaN,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21943688/