我有 df 看起來像這樣:col1 col2 NaN texttext textNaN textNaN text我想在清除值col2,如果NaN如果存在col1。新 df 應(yīng)如下所示:col1 col2 NaN text textNaN NaN
3 回答

縹緲止盈
TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個贊
您正在尋找masking 操作:
df['col2'] = df['col2'].mask(df.col1.isna(), '')
# df['col2'] = np.where(df.col1.isna(), '', df['col2'])
df
col1 col2
0 NaN
1 text text
2 NaN
3 NaN
如果您希望在第二列中使用 NaN 而不是空白,請將第二個參數(shù)省略為mask。

海綿寶寶撒
TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個贊
使用dropna+reindex
df.dropna('col1').reindex(df.index) # fixing by cold :-)
col1 col2
0 NaN NaN
1 text text
2 NaN NaN
3 NaN NaN
添加回答
舉報
0/150
提交
取消