具有如下数据框:
1 Jacke Chan China 1
2 Jason Great 2
3 Statham Britain 2
4 Rock United 3
5 Dwayne States 3
6 Johnson California 3
7 Jack Ma China 4
我需要了解以下信息:
1 Jackie Chan China
2 Jason Statham Great Britain
3 Rock Dwayne Johnson United States California
4 Jack Ma China
答案 0 :(得分:3)
这应该做到:
df = df.groupby(by=[3], as_index=False).agg(' '.join).drop(columns=[3])
print(df)
1 2
0 Jacke Chan China
1 Jason Statham Great Britain
2 Rock Dwayne Johnson United States California
3 Jack Ma China
答案 1 :(得分:1)
您可以尝试以下方法:
df = pd.DataFrame({'name ': {0: 'Jacke Chan',
1: 'Jason',
2: 'Statham',
3: 'Rock',
4: 'Dwayne',
5: 'Johnson',
6: 'Jack Ma'},
'country': {0: 'China',
1: 'Great',
2: 'Britain',
3: 'United',
4: 'States',
5: 'California',
6: 'China'},
'no': {0: 1, 1: 2, 2: 2, 3: 3, 4: 3, 5: 3, 6: 4}})
df.groupby("no").agg(" ".join).reset_index()
答案 2 :(得分:0)
这也将起作用
df_new=df.groupby(3, sort=False)[1].apply(' '.join)
fd_new=df.groupby(3, sort=False)[2].apply(' '.join)