我有一个数据透视表,按月显示客户来电。看起来像这样
CompanyName 1 2 3 4 5 6 7 8 9 10 11 12
Customer 1 17 30 29 39 15 27 15 10 36 21 18 15
Customer 2 4 11 13 22 34 27 32 17 29 31 17 14
Customer 3 10 7 23 21 7 15 25 0 21 9 12 17
Customer 4 6 10 11 8 3 4 3 8 11 11 18 14
Customer 5 13 7 6 12 6 8 2 10 11 7 10 11
我制作了一个日期列表,以便我可以在图表上很好地显示它们。
date_list[::-1]
Out[63]:
['Jul 2015',
'Jun 2015',
'May 2015',
'Apr 2015',
'Mar 2015',
'Feb 2015',
'Jan 2015',
'Dec 2014',
'Nov 2014',
'Oct 2014',
'Sep 2014',
'Aug 2014']
我想重新排序数据透视表,使其与date_list匹配,如下所示:
CompanyName 7 8 9 10 11 12 1 2 3 4 5 6
Customer 1 15 10 36 21 18 15 17 30 29 39 15 27
Customer 2 32 17 29 31 17 14 4 11 13 22 34 27
Customer 3 25 0 21 9 12 17 10 7 23 21 7 15
Customer 4 3 8 11 11 18 14 6 10 11 8 3 4
Customer 5 2 10 11 7 10 11 13 7 6 12 6 8
我正在研究如何重新排序列,但由于这将在月份之前动态变化,我有点困惑。
答案 0 :(得分:1)
假设df
是您的数据透视表。
df
1 2 3 4 5 ... 8 9 10 11 12
CompanyName ...
Customer1 17 30 29 39 15 ... 10 36 21 18 15
Customer2 4 11 13 22 34 ... 17 29 31 17 14
Customer3 10 7 23 21 7 ... 0 21 9 12 17
Customer4 6 10 11 8 3 ... 8 11 11 18 14
Customer5 13 7 6 12 6 ... 10 11 7 10 11
您可以使用pd.concat
来完成这项工作。
pd.concat([df.iloc[:,6:], df.iloc[:,:6]], axis=1)
7 8 9 10 11 ... 2 3 4 5 6
CompanyName ...
Customer1 15 10 36 21 18 ... 30 29 39 15 27
Customer2 32 17 29 31 17 ... 11 13 22 34 27
Customer3 25 0 21 9 12 ... 7 23 21 7 15
Customer4 3 8 11 11 18 ... 10 11 8 3 4
Customer5 2 10 11 7 10 ... 7 6 12 6 8
[5 rows x 12 columns]
答案 1 :(得分:1)
我们假设您的数据透视表名为pt
。
# Months are off by one because of Python's zero based indexing.
months = {'Jan': 0,
'Feb': 1,
'Mar': 2,
'Apr': 3,
'May': 4,
'Jun': 5,
'Jul': 6,
'Aug': 7,
'Sep': 8,
'Oct': 9,
'Nov': 10,
'Dec': 11}
# Get the index value of the first month from your list.
month = months[date_list[0][:3]]
# Now concatenate the results and create you column names.
pt_new = pd.concat([pt.iloc[:, month:], pt.iloc[:, :month]], axis=1)
pt_new.columns = date_list
>>> pt_new
Aug 2014 Sep 2014 Oct 2014 Nov 2014 Dec 2014 Jan 2015
Customer 1 10 36 21 18 15 17
Customer 2 17 29 31 17 14 4
Customer 3 0 21 9 12 17 10
Customer 4 8 11 11 18 14 6
Customer 5 10 11 7 10 11 13
Feb 2015 Mar 2015 Apr 2015 May 2015 Jun 2015 Jul 2015
Customer 1 30 29 39 15 27 15
Customer 2 11 13 22 34 27 32
Customer 3 7 23 21 7 15 25
Customer 4 10 11 8 3 4 3
Customer 5 7 6 12 6 8 2