gpt4 book ai didi

python - 如何根据多列的值使用python pandas将多行合并为一行?

转载 作者:太空宇宙 更新时间:2023-11-04 02:19:46 25 4
gpt4 key购买 nike

我需要将多行合并为一行,原始数据框如下所示:

IndividualID    DayID    TripID    JourSequence   TripPurpose
200100000001 1 1 1 3
200100000001 1 2 2 31
200100000001 1 3 3 23
200100000001 1 4 4 5
200100000009 1 55 1 3
200100000009 1 56 2 12
200100000009 1 57 3 4
200100000009 1 58 4 6
200100000009 1 59 5 19
200100000009 1 60 6 2

我试图建立某种“旅行链”,所以基本上一个人在一天中的所有旅行顺序和旅行目的都应该在同一行中......

理想情况下,我试图将表格转换为如下形式:

IndividualID    DayID     Seq1   TripPurp1     Seq2   TripPur2     Seq3   TripPurp3     Seq4   TripPur4
200100000001 1 1 3 2 31 3 23 4 5
200100000009 1 1 3 2 12 3 4 4 6

如果不行,那么下面的模式也行:

IndividualID    DayID      TripPurposes
200100000001 1 3, 31, 23, 5
200100000009 1 3, 12, 4, 6

有没有可能的解决方案?我在考虑 for loop/while 语句,但也许这不是一个好主意。提前致谢!

最佳答案

你可以试试:

df_out = df.set_index(['IndividualID','DayID',df.groupby(['IndividualID','DayID']).cumcount()+1]).unstack().sort_index(level=1, axis=1)
df_out.columns = df_out.columns.map('{0[0]}_{0[1]}'.format)
df_out.reset_index()

输出:

   IndividualID  DayID  JourSequence_1  TripID_1  TripPurpose_1  \
0 200100000001 1 1.0 1.0 3.0
1 200100000009 1 1.0 55.0 3.0

JourSequence_2 TripID_2 TripPurpose_2 JourSequence_3 TripID_3 \
0 2.0 2.0 31.0 3.0 3.0
1 2.0 56.0 12.0 3.0 57.0

TripPurpose_3 JourSequence_4 TripID_4 TripPurpose_4 JourSequence_5 \
0 23.0 4.0 4.0 5.0 NaN
1 4.0 4.0 58.0 6.0 5.0

TripID_5 TripPurpose_5 JourSequence_6 TripID_6 TripPurpose_6
0 NaN NaN NaN NaN NaN
1 59.0 19.0 6.0 60.0 2.0

关于python - 如何根据多列的值使用python pandas将多行合并为一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51901068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com