[英]Pandas - Group and then sort
I would like to group and sort a DataFrame so: 我想对DataFrame进行分组和排序,以便:
I kind of managed to do this in multiple lines but there must be a more pythonic/pandas way. 我有点设法在多行中做到这一点,但必须有更多的pythonic / pandas方式。
The goal of this is not only to display the table but also be able to pass it to tools like plot.ly to do a stacked barchart with the region with more counts first and then each name from the largest to the smallest stacked. 这样做的目的不仅是显示表格,而且还能够将其传递给plot.ly等工具,以便首先使用具有更多计数的区域进行堆叠条形图,然后将每个名称从最大堆叠到最小堆叠。
So this: 所以这:
A B C
Region1 Name1 1
Region1 Name1 1
Region1 Name2 1
Region2 Name3 1
Region2 Name4 1
Region2 Name4 1
Region2 Name4 1
Region3 Name5 1
Region3 Name6 1
Would become: 会成为:
A B Count C
Region2 Name4 3
Name3 1
Region1 Name1 2
Name2 1
Region3 Name5 1
Name6 1
(Sorry the format is not nice but this should be the right version so you can copy/paste tabular data) (抱歉,格式不太好,但这应该是正确的版本,因此您可以复制/粘贴表格数据)
Code to create the first table: 用于创建第一个表的代码:
df = pd.DataFrame({'A': {0: 'Region1',
1: 'Region1',
2: 'Region1',
3: 'Region2',
4: 'Region2',
5: 'Region2',
6: 'Region2',
7: 'Region3',
8: 'Region3'},
'B': {0: 'Name1',
1: 'Name1',
2: 'Name2',
3: 'Name3',
4: 'Name4',
5: 'Name4',
6: 'Name4',
7: 'Name5',
8: 'Name6'},
'C': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1}})
Umm, not sure if this is the best way but I am using sort_values
and pd.Categorical
: 嗯,不确定这是不是最好的方法,但我使用的是
sort_values
和pd.Categorical
:
m=(df.groupby(['A','B'])['C'].size().reset_index().sort_values('C',ascending=False)
.reset_index(drop=True))
m.A = pd.Categorical(m.A,m.A.unique(),ordered=True)
m.sort_values('A').reset_index(drop=True)
A B C
0 Region2 Name4 3
1 Region2 Name3 1
2 Region1 Name1 2
3 Region1 Name2 1
4 Region3 Name5 1
5 Region3 Name6 1
The sorting seems to happen on the maximum count by region group, then by the count by region - name group. 排序似乎发生在按地区组的最大计数,然后按地区 - 名称组计数。
To get the desired result, you need to calculate the max / region group & then hide this column before displaying, which you can do like this 要获得所需的结果,您需要计算最大/区域组,然后在显示之前隐藏此列,您可以这样做
df2 = df.groupby(['A', 'B']).agg('count')
df2['maxA'] = df2.groupby('A').C.transform('max')
df2.sort_values(['maxA', 'C'], ascending=[False, False])[['C']]
# produces the following output:
C
A B
Region2 Name4 3
Name3 1
Region1 Name1 2
Name2 1
Region3 Name5 1
Name6 1
I would do 2 steps: 我会做两个步骤:
Step 1: create the mask index of sorted A
by count 步骤1:按计数创建排序
A
的掩码索引
Step 2: use .loc
to re-order the df
and groupby
with sort=False
and call value_counts
步骤2:使用
.loc
以sort=False
重新排序df
和groupby
并调用value_counts
m = df.groupby('A').A.transform('count').sort_values(ascending=False).index
df.loc[m].groupby('A', sort=False).B.value_counts().to_frame('Count')
Out[200]:
Count
A B
Region2 Name4 3
Name3 1
Region1 Name1 2
Name2 1
Region3 Name5 1
Name6 1
region_totals = df.groupby('A')['C'].sum()
mapping = dict(zip(region_totals.index, region_totals.values))
df['D'] = df["A"].map(mapping)
df2 = df.groupby(['D','A','B']).sum().sort_values(['D','C'], ascending =False)
df2.index = df2.index.droplevel(0)
df2.rename(columns={'C':'Count'}
Count
A B
Region2 Name4 3
Name3 1
Region1 Name1 2
Name2 1
Region3 Name5 1
Name6 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.