【发布时间】:2020-09-24 10:32:35
【问题描述】:
我有以下数据框:
>>>name breakfast lunch dinner
0 Zoey apple egg noodels
1 Rena pear pasta
2 Shila tomato potatoes
3 Daphni coffee soup
4 Dufi
我想创建一个新列,其中包含每个名字在同一天吃的所有食物值。我尝试使用“+”来做到这一点,并用“,”分隔单词,如下所示:
df['food']=df['breakfast']+','+df['lunch']+','+df['dinner']
但是如果我有空值,我在中间有',':
>>>name breakfast lunch dinner food
0 Zoey apple egg noodels apple,egg,noodels
1 Rena pear pasta pear,,pasta
2 Shila tomato potatoes ,tmatoe,potatoes
3 Daphni coffee soup coffee,,soupp
4. Dufi ,,
我想在正确的地方用 ',' 把它弄干净,例如不要放 ,如果有空:
>>>name breakfast lunch dinner food
0 Zoey apple egg noodels apple,egg,noodels
1 Rena pear pasta pear,pasta
2 Shila tomato potatoes tmatoe,potatoes
3 Daphni coffee soup coffee,soup
4 Dufi
有没有办法做到这一点?定义如果有空单元格不要添加它/不要放在错误的地方
【问题讨论】: