递归地将文本从一行添加到另一行

时间:2018-11-15 18:28:02

标签: python pandas parsing dataframe

我想使用条件连接将文本从一行添加到另一行。这是一个示例数据集:

import pandas as pd

df = pd.DataFrame({'ID' : ['A','A','A','B','B','B','B','B'],
                   'Meal' : [1,2,3,1,2,3,4,5],
                   'Solo' : [1,0,1,1,0,0,0,0], 
                   'Dependency' : [0,1,0,0,1,2,2,1],
                   'Food': ['Steak','Eggs and meal 1','Lamb','Chicken',
                            'Steak and meal 1 with eggs','Soup and meal 2',
                            'Water and meal 2 with meal 1','Ham with meal 1']                  
                  })

结果数据框:

  ID  Meal  Solo  Dependency                          Food
0  A     1     1           0                         Steak
1  A     2     0           1               Eggs and meal 1
2  A     3     1           0                          Lamb
3  B     1     1           0                       Chicken
4  B     2     0           1    Steak and meal 1 with eggs
5  B     3     0           2               Soup and meal 2
6  B     4     0           2  Water and meal 2 with meal 1
7  B     5     0           1               Ham with meal 1

我想创建一列包含进餐信息的列:

  ID  Meal                               Combined
0  A     1                                  Steak
1  A     2                         Eggs and Steak
2  A     3                                   Lamb
3  B     1                                Chicken
4  B     2            Steak and Chicken with eggs
5  B     3   Soup and Steak and Chicken with eggs
6  B     4  Water and Steak and Chicken with eggs
7  B     5                       Ham with Chicken

任何帮助将不胜感激。

谢谢!

0 个答案:

没有答案