rwj*_*jam 2 python dataframe pandas
有没有办法扫描 python 中的数据帧来创建一个新的数据帧,该数据帧按特定列分组,删除重复项,同时保存不相似的条目,比如保存到列表中?
所以如果我有一个看起来像这样的数据框......
Genre Rating CustomRating
Thriller 5 5
Thriller 5 5
Comedy 9 9
Action 3 6
Action 2 7
Run Code Online (Sandbox Code Playgroud)
我需要它变成这样的东西......
Genre Rating CustomRating
Thriller 5 5
Comedy 9 9
Action 3, 2 6, 7
Run Code Online (Sandbox Code Playgroud)
进度更新
@ignoring_gravity 建议所做的工作效果df.drop_duplicates().groupby('Genre', sort=False).agg(list)很好,但是有没有一种方法可以返回字符串或整数而不是列表的项目?
你可以groupby这样做,然后agg:
df.groupby('Genre', sort=False).agg(lambda x: list(set(x))).reset_index()
Run Code Online (Sandbox Code Playgroud)
你会得到
Genre Rating CustomRating
0 Thriller [5] [5]
1 Comedy [9] [9]
2 Action [2, 3] [6, 7]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1853 次 |
| 最近记录: |