假设我们有如下数据:
A B
123 John
456 Mary
102 Allen
456 Nickolan
123 Richie
167 Daniel
如果重复,我们希望根据 A 列检索行,然后使用该代码名称存储在不同的数据框中。
[123 John, 123 Richie], These both will be stored in df_123
[456 Mary, 456 Nickolan], These both will be stored in df_456
[102 Allen] will be stored in df_102
[167 Daniel] will be stored in df_167
提前致谢
最佳答案
group 然后使用列表理解,这将返回基于组的数据帧列表:
group = df.groupby('A')
dfs = [group.get_group(x) for x in group.groups]
[ A B
2 112 Allen
5 112 Daniel, A B
0 123 John
4 123 Richie, A B
1 456 Mary
3 456 Nickolan]
关于python - 如何根据 Pandas Python 中特定列中的重复值检索行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53452096/