【发布时间】:2017-05-17 21:38:58
【问题描述】:
我有一个包含两列的 df,我想合并两列而忽略 NaN 值。问题是有时两列都有 NaN 值,在这种情况下,我希望新列也有 NaN。示例如下:
df = pd.DataFrame({'foodstuff':['apple-martini', 'apple-pie', None, None, None], 'type':[None, None, 'strawberry-tart', 'dessert', None]})
df
Out[10]:
foodstuff type
0 apple-martini None
1 apple-pie None
2 None strawberry-tart
3 None dessert
4 None None
我尝试使用fillna 来解决这个问题:
df['foodstuff'].fillna('') + df['type'].fillna('')
我得到了:
0 apple-martini
1 apple-pie
2 strawberry-tart
3 dessert
4
dtype: object
第 4 行已成为空白值。在这种情况下我不想要的是 NaN 值,因为两个组合列都是 NaN。
0 apple-martini
1 apple-pie
2 strawberry-tart
3 dessert
4 None
dtype: object
【问题讨论】:
标签: python pandas dataframe nonetype