gpt4 book ai didi

python - 如何根据 Pandas Python 中特定列中的重复值检索行?

转载 作者:太空宇宙 更新时间:2023-11-04 09:36:35 25 4
gpt4 key购买 nike

假设我们有如下数据:

 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com