Pandas Groupby Agg函数中的列顺序 [英] Column Order in Pandas Groupby Agg Function
本文介绍了Pandas Groupby Agg函数中的列顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有一种自动的方法来维护返回的数据帧的列顺序("C","B","A")?
Is there an automated way to maintain the order of the columns ('C', 'B', 'A') for the dataframe that is returned?
g = df.groupby(['people'])
g['people'].agg({'C' : len,
'B' : len,
'A' : len,
})
这将返回a列,分别为A,B,C,而不是C,B,A.
This will return a the columns as A, B, C rather than C, B, A.
我只能找到示例,但找不到agg函数本身的文档.
I can only find examples but not the documentation for the agg function itself.
这似乎是一种解决方法:
This seems to be a workaround:
g = df.groupby(['people'])
g['people'].agg({'C' : len,
'B' : len,
'A' : len,
}).reindex_axis(['C','B','A'], axis=1)
推荐答案
OrderedDict与pandas-0.18.0-py2.7配合使用令人惊讶:
OrderedDict worked surprisingly with pandas-0.18.0-py2.7:
from collections import OrderedDict
g = df.groupby(['people'])
g['people'].agg( OrderedDict([
('C' , len),
('B' , len),
('A' , len),
]) )
这篇关于Pandas Groupby Agg函数中的列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文