我不知道该怎么称呼这个。
假设以下Pandas DataFrame:
Student ID Class
1 John 99124 Biology
2 John 99124 History
3 John 99124 Geometry
4 Sarah 74323 Physics
5 Sarah 74323 Geography
6 Sarah 74323 Algebra
7 Alex 80045 Trigonometry
8 Alex 80045 Economics
9 Alex 80045 French我希望通过创建每个学生要参加的类的列表来减少DataFrame中的行数,然后将其放在"class“列中。下面是我想要的输出:
Student ID Class
1 John 99124 ["Biology","History","Geometry"]
2 Sarah 74323 ["Physics","Geography","Algebra"]
3 Alex 80045 ["Trigonometry","Economics","French"]我使用的是一个大型DataFrame,它的组织不如本例那么好。任何帮助都是非常感谢的。
发布于 2019-08-02 19:25:05
发布于 2019-08-02 19:25:33
df.groupby('ID')['Class'].apply(list)发布于 2019-08-02 19:29:38
让我们看看,使用一些帮助Apply multiple functions to multiple groupby columns
你可以写点什么
df= df.groupby('student').agg({'id':'max', 'Class': lambda x: x.tolist()})希望能帮上忙,朱利奥
https://stackoverflow.com/questions/57332350
复制相似问题