排序/" ORDER BY"相当于Pandas Python GroupBy Object,Aggregate

时间:2018-02-21 17:29:42

标签: python pandas sorting dataframe pandas-groupby

我到处寻找并且无法找到适合我的明确解决方案。假设我有下表:

    Company or Agency                                       Records Exposed
                                                    count   sum
0   1-800-Flowers.com                               1       Unknown
1   21st Century Oncology                           1       2,213,597
2   37th Parallel Properties Investment Group, LLC  1       Unknown
3   4D Sound Diagnostics                            1       1,000
4   7-Eleven, Inc.                                  1       7,832

使用以下查询生成:

companies = data.groupby('Company or Agency')
companies = companies.agg({'Records Exposed': np.sum, 'Company or Agency' : ['count']})
companies = companies.reset_index()
companies = pd.DataFrame(companies)
#companies.sort_values('Company or Agency')
companies.head()

基本上,我想根据列公司或代理商数量对我的DataFrame进行排序。'我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

看起来agg函数创建了MultiIndex。引用多索引DataFrame的标头的一种方法是通过元组,所以它总是应该如下所示:

companies.sort_values(by=('Company or Agency', 'count'))