python - 仅当没有单元格为空时,才在它们之间添加带有“,”的两列值
问题描述
我有以下数据框:
>>>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
有什么办法吗?定义如果有空单元格不添加/不放,在错误的地方
解决方案
在您的索引上使用.stack
with 。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
推荐阅读
- html - 在 html select 中设置一个不可选择的选项
- tinymce - 禁用对 tinymce 模式更改的自动对焦
- android - programm NFC Card inside the phone Android and Ios
- java - 克服 Java 中的类型擦除
- sonarqube - Sonarqube 6.7.4:如何将默认登录页面从 /about 更改为 /projects
- python - pip3:错误的解释器:没有这样的文件或目录
- html - 将 HTML 作为图像渲染到 Node.js 中的 Buffer 中
- c# - Get RowIndex from nested listview button click - UWP
- python - tf.optimizer 更新稀疏输入的所有现有权重
- firebase - Do download URLs of Firebase Storage objects live forever?