pandas combine two columns with null values

后端 未结 6 1961
醉梦人生
醉梦人生 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:39

    Use fillna on one column with the fill values being the other column:

    df['foodstuff'].fillna(df['type'])
    

    The resulting output:

    0      apple-martini
    1          apple-pie
    2    strawberry-tart
    3            dessert
    4               None
    

提交回复
热议问题