首页 > 解决方案 > 仅当没有单元格为空时,才在它们之间添加带有“,”的两列值

问题描述

我有以下数据框:

>>>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                  

有什么办法吗?定义如果有空单元格不添加/不放,在错误的地方

标签: pythonpandascelladd

解决方案


在您的索引上使用.stackwith 。groupby

假设你的空白实际上是真正的空值

因为,我们不需要名称,我们可以将其添加到索引中或删除它,我已将其添加到此处。

df['food'] = df.set_index('name',append=True).stack().groupby(level=0).agg(','.join)

如果你的空白不是空值,我们可以做

df.replace(' ', np.nan).set_index('name',append=True).stack()\
                       .groupby(level=0).agg(','.join)

    name breakfast     lunch   dinner               food
0    Zoey     apple       egg  noodels  apple,egg,noodels
1    Rena      pear     pasta      NaN         pear,pasta
2   Shila    tomato  potatoes      NaN    tomato,potatoes
3  Daphni    coffee      soup      NaN        coffee,soup
4    Dufi       NaN       NaN      NaN                NaN

推荐阅读