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

如何根据Python中特定列中的重复值检索行?,python,python-3.x,pandas,dataframe,pandas-groupby,Python,Python 3.x,Pandas,Dataframe,Pandas Groupby,假设我们有如下数据: 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 bo

假设我们有如下数据:

 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 = 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]

分组,然后使用列表理解,它将返回基于组的数据帧列表:

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]
+ + 不建议创建数量可变的变量。您可以使用字典:

dfs = dict(tuple(df.groupby('A')))
就这样。要访问A==123的数据帧,请使用dfs[123]等

注意,您的数据帧现在是不同的对象。如果没有Python级别的循环,您不能再对dfs执行操作并将其应用于每个数据帧值。

++ 不建议创建数量可变的变量。您可以使用字典:

dfs = dict(tuple(df.groupby('A')))
就这样。要访问A==123的数据帧,请使用dfs[123]等


注意,您的数据帧现在是不同的对象。如果没有Python级别的循环,您不能再对dfs执行操作并将其应用于每个数据帧值。

dfs={'df_{}'。formatk:v代表k,df中的v.groupby'a'如果lenv>1}可能会有帮助。感谢您的帮助,它对重复项有效。但是仍然需要对行进行一些更改,包括重复的行和唯一的行。dfs={'df_{}。formatk:v代表k,df中的v。groupby'A'如果lenv>1}可能会有所帮助。感谢您的帮助,它对重复的行有效。但是仍然需要对行进行一些更改,包括重复和唯一。我非常喜欢这种方法我非常喜欢这种方法