熊猫将多索引dafaframe的索引重置为日期值,并将其他索引转换为列

更多

我有一个多索引数据框,如下所示:

                                                                   number
location                   category         created_on  
Arab Republic of Egypt      ACCESS          2018-06-25 00:00:00        4
                            ACCOUNT         2018-04-24 04:00:00        3
                                            2018-05-31 04:00:00        3
                                            2018-06-28 00:00:00        3
                        ACTIVE DIRECTORY    2018-04-01 00:00:00        3
... ... ... ...
United States of America    WINDOWS 10      2018-09-25 04:00:00        8
                                            2018-09-25 08:00:00       13
                                            2018-09-26 08:00:00       12
                                            2018-09-27 08:00:00        8
                                            2018-09-27 12:00:00        9

我想将其隐藏到以日期列为索引的数据框,但又不丢失列号中的计数,新的数据框应如下所示:

created_on              number       category       location
2018-06-25 00:00:00          4       ACCESS         Arab Republic of Egypt
2018-04-24 04:00:00          3       ACCOUNT        Arab Republic of Egypt
2018-05-31 04:00:00          3       ACCOUNT        Arab Republic of Egypt
2018-06-28 00:00:00          3       ACCOUNT        Arab Republic of Egypt
2018-04-01 00:00:00          3  ACTIVE DIRECTORY    Arab Republic of Egypt     
... ... ... ...
2018-09-25 04:00:00          8     WINDOWS 10       United States of America
2018-09-25 08:00:00         13     WINDOWS 10       United States of America
2018-09-26 08:00:00         12     WINDOWS 10       United States of America
2018-09-27 08:00:00          8     WINDOWS 10       United States of America
2018-09-27 12:00:00          9     WINDOWS 10       United States of America

我怎样才能做到这一点?

Shubham Sharma

使用swaplevel上的水平02再利用reset_index的水平12

df1 = df.swaplevel(0, 2).reset_index(level=[1, 2])

或先使用另一个想法,reset_index然后set_index在column上使用created_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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将索引转换为以熊猫为单位的日期时间

将熊猫系列的多索引转换为数据框列

将熊猫索引转换为日期时间

将熊猫中的日期时间索引转换为单独的列

熊猫表-将多索引转换为单索引

将多索引熊猫数据框转换为单索引

熊猫将第一个多索引转换为行索引,将第二个多索引转换为列索引

将索引转换为列 - 熊猫

将列转换为熊猫索引

将多索引键转换为列

将所有列设置为索引,或将数据帧转换为多索引系列

将多列转换为唯一的列并将其与其他列信息匹配

重置列索引熊猫?

熊猫数据重塑,根据发生率将索引相同但值不同的多行转换为多列

将数据帧的熊猫连接转换为多索引

ValueError 将多索引熊猫数据框转换为 Excel

将大熊猫(多)索引解析为日期时间

熊猫数据框groupby索引并将行值转换为列

将多级熊猫索引转换为日期时间

如何将熊猫中的索引转换为日期时间?

熊猫将小时索引整数转换为日期时间

Python将日期和时间转换为熊猫索引

将熊猫系列索引转换为日期时间

熊猫将部分列索引转换为日期时间

通过使用第二个索引作为列将熊猫多索引系列转换为数据框

使用多索引列将2D数据转换为1D的熊猫

使用熊猫将具有匹配索引的多行动态转换为多列

熊猫:将某行中某列的值设置为存储在其他df中其其他行的索引处的值

大熊猫将索引值转换为小写