我有以下代码:Python的熊猫数据透视表按日期排序
data_df = pandas.read_csv(filename, parse_dates = True)
groupings = np.unique(data_df[['Ind']])
for group in groupings:
data_df2 = data_df[data_df['Ind'] == group]
table = pandas.pivot_table(data_df2, values='Rev', index=['Ind', 'Month'], columns=['Type'], aggfunc=sum)
table = table.sort_index(ascending=[0, 0])
print(table)
我如何通过月份和年份(如排序枢轴“表”当我打印“表”我想DEC-14成为每个组的输出的第一行)?
下面是“data_df”数据的样本:
Ind Type Month Rev
0 A Voice Dec-14 10.00
1 A Voice Jan-15 8.00
2 A Voice Feb-15 13.00
3 A Voice Mar-15 9.00
4 A Voice Apr-15 11.00
5 A Voice May-15 14.00
6 A Voice Jun-15 6.00
7 A Voice Jul-15 4.00
8 A Voice Aug-15 12.00
9 A Voice Sep-15 7.00
10 A Voice Oct-15 5.00
11 A Elec Dec-14 8.04
12 A Elec Jan-15 6.95
13 A Elec Feb-15 7.58
14 A Elec Mar-15 8.81
15 A Elec Apr-15 8.33
16 A Elec May-15 9.96
17 A Elec Jun-15 7.24
18 A Elec Jul-15 4.26
19 A Elec Aug-15 10.84
20 A Elec Sep-15 4.82
21 A Elec Oct-15 5.68
22 B Voice Dec-14 10.00
23 B Voice Jan-15 8.00
24 B Voice Feb-15 13.00
25 B Voice Mar-15 9.00
26 B Voice Apr-15 11.00
27 B Voice May-15 14.00
28 B Voice Jun-15 6.00
29 B Voice Jul-15 4.00
.. .. ... ... ...
输出(我是用升玩,但只希望排序阿尔法):
Type Elec Voice
Ind Month
A Sep-15 4.82 7
Oct-15 5.68 5
May-15 9.96 14
Mar-15 8.81 9
Jun-15 7.24 6
Jul-15 4.26 4
Jan-15 6.95 8
Feb-15 7.58 13
Dec-14 8.04 10
Aug-15 10.84 12
Apr-15 8.33 11
我想输出按日期排序:
Type Elec Voice
Ind Month
A Dec-14 8.04 10
Jan-15 6.95 8
Feb-15 7.58 13
...
精彩和感谢!我不得不更新我的代码中的sort_index,它完美地工作。 (table = table.sort_index(ascending = [0,1])) –