[英]How do I join the values of two columns in a Pandas dataframe only if none are NaN
[英]How to join two column in dataframe by overwriting only NaN values in pandas?
我試圖通過僅覆蓋第二列中的NaN值來連接兩列。 我已經嘗試了多種方法,但是沒有任何效果。
New.Market.Cap New.Market.Cap2 Expected.Output
Date Symbol
2017-01-01 BTC 4.467053e+09 NaN 4.467053e+09
ETH 7.148243e+08 6.059076e+08 6.059076e+08
XRP 3.633730e+08 NaN 3.633730e+08
2017-01-02 BTC 4.575871e+09 NaN 4.575871e+09
ETH 7.334621e+08 6.249679e+08 6.249679e+08
XRP 3.633730e+08 NaN 3.633730e+08
我已經嘗試了多種方法,但是無法使其正常工作。
使用Series.combine_first
或Series.fillna
:
df['Expected.Output'] = df['New.Market.Cap2'].combine_first(df['New.Market.Cap'])
要么:
df['Expected.Output'] = df['New.Market.Cap2'].fillna(df['New.Market.Cap'])
如果需要,還可以刪除列DataFrame.pop
是您的朋友:
df['Expected.Output'] = df.pop('New.Market.Cap2').combine_first(df.pop('New.Market.Cap'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.