【发布时间】:2017-12-18 04:09:14
【问题描述】:
我有两个数据框,需要根据日期合并它们,但应该分别为每个组 (participant_id) 进行合并。
df1:
response_date summary epis_mark participant_id
0 2012-01-04 0.0 False 13
1 2012-01-11 0.0 False 13
2 2012-01-19 0.0 False 13
3 2012-01-29 0.0 False 13
4 2012-02-02 0.0 False 13
0 2012-01-02 8.0 True 14
1 2012-01-10 5.0 False 14
2 2012-01-18 2.0 False 14
3 2012-01-24 1.0 False 14
4 2012-01-31 2.0 False 14
0 2012-01-07 4.0 False 17
1 2012-01-11 NaN False 17
2 2012-01-18 4.0 False 17
3 2012-01-25 NaN False 17
4 2012-02-01 NaN False 17
df2:
response_date summary epis_mark participant_id
0 2012-01-04 17.0 True 13
1 2012-01-11 18.0 True 13
2 2012-01-19 16.0 True 13
3 2012-01-29 15.0 True 13
4 2012-02-02 15.0 True 13
0 2012-01-02 12.0 True 14
1 2012-01-10 8.0 True 14
2 2012-01-18 21.0 True 14
3 2012-01-24 19.0 True 14
4 2012-01-31 20.0 True 14
0 2012-01-04 NaN False 17
1 2012-01-11 NaN False 17
2 2012-01-18 NaN False 17
3 2012-01-25 NaN False 17
4 2012-02-01 NaN False 17
我需要获取一个数据帧 (wide),其中每个 participant_id 的合并是在 response date 上独立完成的。比如:
>> pd.merge(df1[df1.participant_id == i], df2[df2.participant_id == i], on='response_date', how='outer')
但不循环 i 并使用 groupby。
【问题讨论】:
标签: python pandas merge pandas-groupby