1 回答

TA贡献1803条经验 获得超3个赞
swaplevel在级别上使用0,2然后reset_index在级别1和上使用2:
df1 = df.swaplevel(0, 2).reset_index(level=[1, 2])
或者另一个想法先使用然后在列上reset_index使用:set_indexcreated_at
df1 = df.reset_index().set_index('created_on')
结果:
print(df1)
category location number
created_on
2018-06-25 00:00:00 ACCESS Arab Republic of Egypt 4
2018-04-24 04:00:00 ACCOUNT Arab Republic of Egypt 3
2018-05-31 04:00:00 ACCOUNT Arab Republic of Egypt 3
2018-06-28 00:00:00 ACCOUNT Arab Republic of Egypt 3
2018-04-01 00:00:00 ACTIVE DIRECTORY Arab Republic of Egypt 3
...
2018-09-25 04:00:00 WINDOWS 10 United States of America 8
2018-09-25 08:00:00 WINDOWS 10 United States of America 13
2018-09-26 08:00:00 WINDOWS 10 United States of America 12
2018-09-27 08:00:00 WINDOWS 10 United States of America 8
2018-09-27 12:00:00 WINDOWS 10 United States of America 9
添加回答
举报