熊猫合并多行和多列

时间:2020-07-12 04:19:03

标签: python pandas pandas-groupby

嗨,我有一个像这样的数据框:

index event  action   date
0     event1 action1  date1
1     event2 action2  date1
2     event3 action3  date2
3     event4 action4  date2

我想合并列和行。我已经使用groupbylambda function合并了行,不能通过合并行和列来获得类似结果的输出

index  final_actions               date
0      for event1 do action1 and   date1
       for event2 do action2 
1      for event3 do action3 and   date2
       for event4 do action4

非常感谢您的帮助

1 个答案:

答案 0 :(得分:0)

尝试一下,groupby + zip

(
    df.groupby('date')['event', 'action']
        .apply(lambda x:
               " and ".join([f"for {x} do {y}"
                             for x, y in zip(x['event'], x['action'])]))
)