pandas combine two columns with null values

后端 未结 6 1944
醉梦人生
醉梦人生 2021-02-03 22:10

I have a df with two columns and I want to combine both columns ignoring the NaN values. The catch is that sometimes both columns have NaN values in which case I want the new co

6条回答
  •  别那么骄傲
    2021-02-03 22:32

    • fillna both columns together
    • sum(1) to add them
    • replace('', np.nan)

    df.fillna('').sum(1).replace('', np.nan)
    
    0      apple-martini
    1          apple-pie
    2    strawberry-tart
    3            dessert
    4                NaN
    dtype: object
    

提交回复
热议问题