简体   繁体   English

熊猫 - 按组成员编号排序

[英]Pandas - Sort by group membership numbers

When faced with large numbers of groups, any graph you might make is apt to be useless due to having too many lines and an unreadable legend. 当面对大量的群体时,由于线条太多且传说难以理解,您可能制作的任何图形都会变得无用。 In these cases, being able to find the groups that have the most and least information in them is very useful. 在这些情况下,能够找到其中包含最多和最少信息的组非常有用。 However, while x.size() tells you the group membership (after having used groupby ), there is no way I can find to re-sort the dataframe using this information, so that you can then use limiting looping to only graph the first x groups. 但是,虽然x.size()告诉您组成员资格(在使用groupby ),但我无法使用此信息重新排序数据框,因此您可以使用限制循环来仅绘制第一个图形x组。

You can use transform to get the counts and sort on that column: 您可以使用transform来获取该列的计数和排序:

df = pd.DataFrame({'A': list('aabababc'), 'B': np.arange(8)})
df
Out: 
   A  B
0  a  0
1  a  1
2  b  2
3  a  3
4  b  4
5  a  5
6  b  6
7  c  7

df['counts'] = df.groupby('A').transform('count')
df
Out: 
   A  B  counts
0  a  0       4
1  a  1       4
2  b  2       3
3  a  3       4
4  b  4       3
5  a  5       4
6  b  6       3
7  c  7       1

Now you can sort by counts : 现在您可以按counts排序:

df.sort_values('counts')
Out: 
   A  B  counts
7  c  7       1
2  b  2       3
4  b  4       3
6  b  6       3
0  a  0       4
1  a  1       4
3  a  3       4
5  a  5       4

In one line: 在一行中:

df.assign(counts = df.groupby('A').transform('count')).sort_values('counts')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM