groupby ,然后将特定行转换为同一数据框中的列

groupby , Then convert the specific rows to coulmns in the same dataframe

首先我有这个数据框:

ID Age name time
0 1 12 r y
1 1 13 c y
2 1 14 n y
3 1 15 m y
4 2 11 l N
5 2 22 k N
6 2 33 r N
7 2 55 l N

这是我的 expected/desired 输出:

ID Age 1 Age 2 Age 3 name time
0 1 12 13 15 m Y
1 2 11 22 55 l N

试试 groupbypivot:

#keep only the needed data
grouped = df.groupby("ID", as_index=False).agg({"Age": lambda x: x.tolist()[:2]+[x.iat[-1]], "name": "last", "time": "last"}).explode("Age")

#get the count for the age columns
grouped["idx"] = grouped.groupby("ID").cumcount().add(1)

#pivot to get the required structure
output = grouped.pivot(["ID","name","time"],"idx","Age").add_prefix("Age").reset_index().rename_axis(None, axis=1)

>>> output
   ID name time Age1 Age2 Age3
0   1    m    y   12   13   15
1   2    l    N   11   22   55
 df1 = df.groupby('ID').agg({'Age':lambda x:list(np.r_[x.head(2),x.tail(1)])})

df1[['name', 'time']] = df.groupby('ID')[['name', 'time']].last()

df1[['Age1', 'Age2', 'Age3']] = pd.DataFrame(df1['Age'].to_list(), index = df1.index)

df1.drop('Age', axis = 1).reset_index()

   ID name time  Age1  Age2  Age3
0   1    m    y    12    13    15
1   2    l    N    11    22    55