我创建了一个数据透视表,根据其中一列和时间索引的月份进行分组。
这是数据透视表
WorkerThread
现在我希望2月3月和4月按时间顺序排列,而不是按字母顺序排列
尝试以这种方式重新索引:
AWRT AWFT AWDT
Time type
April All 38.190119 65.789103 27.598984
DHW 19.676627 60.889196 41.212569
SH 47.342757 61.335566 13.992809
February All 43.896487 57.982944 14.086457
SH 40.864670 50.567133 9.702463
March All 42.083836 69.139818 27.055982
DHW 18.908873 62.936898 44.028024
SH 52.249342 70.013904 17.764563
我获得以下内容,它不再是数据透视表:
new_index=[['February', 'March', 'April'], ['All', 'DHW', 'SH']]
df1=df1.reindex(new_index)
我也尝试直接访问数据透视表索引的标签,但我被告知这些是不可变的。
提前感谢您的帮助
答案 0 :(得分:2)
仅重新索引 相关级别(level=0
):
import pandas as pd
idx = pd.MultiIndex(levels=[['April', 'February', 'March'], ['All', 'DHW', 'SH']],
labels=[[0, 0, 0, 1, 1, 2, 2, 2], [0, 1, 2, 0, 2, 0, 1, 2]],
names=['Time', 'type'])
df = pd.DataFrame([[38.190119, 65.789103, 27.598984],
[19.676627, 60.889196, 41.212569],
[47.342757, 61.335566, 13.992809],
[43.896487, 57.982944, 14.086457],
[40.864670, 50.567133, 9.702463],
[42.083836, 69.139818, 27.055982],
[18.908873, 62.936898, 44.028024],
[52.249342, 70.013904, 17.764563]],
columns=['AWRT', 'AWFT', 'AWDT'],
index=idx)
print(df)
# AWRT AWFT AWDT
# Time type
# April All 38.190119 65.789103 27.598984
# DHW 19.676627 60.889196 41.212569
# SH 47.342757 61.335566 13.992809
# February All 43.896487 57.982944 14.086457
# SH 40.864670 50.567133 9.702463
# March All 42.083836 69.139818 27.055982
# DHW 18.908873 62.936898 44.028024
# SH 52.249342 70.013904 17.764563
print(df.reindex(['February', 'March', 'April'], level=0))
# AWRT AWFT AWDT
# Time type
# February All 43.896487 57.982944 14.086457
# SH 40.864670 50.567133 9.702463
# March All 42.083836 69.139818 27.055982
# DHW 18.908873 62.936898 44.028024
# SH 52.249342 70.013904 17.764563
# April All 38.190119 65.789103 27.598984
# DHW 19.676627 60.889196 41.212569
# SH 47.342757 61.335566 13.992809