亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

pandas 將多索引 dafaframe 的索引重置為日期值并將其他索引轉換為列

pandas 將多索引 dafaframe 的索引重置為日期值并將其他索引轉換為列

月關寶盒 2023-04-11 16:11:56
我有一個多索引數據框,如下所示:                                                                   numberlocation                   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       location2018-06-25 00:00:00          4       ACCESS         Arab Republic of Egypt2018-04-24 04:00:00          3       ACCOUNT        Arab Republic of Egypt2018-05-31 04:00:00          3       ACCOUNT        Arab Republic of Egypt2018-06-28 00:00:00          3       ACCOUNT        Arab Republic of Egypt2018-04-01 00:00:00          3  ACTIVE DIRECTORY    Arab Republic of Egypt     ... ... ... ...2018-09-25 04:00:00          8     WINDOWS 10       United States of America2018-09-25 08:00:00         13     WINDOWS 10       United States of America2018-09-26 08:00:00         12     WINDOWS 10       United States of America2018-09-27 08:00:00          8     WINDOWS 10       United States of America2018-09-27 12:00:00          9     WINDOWS 10       United States of America我怎樣才能做到這一點?
查看完整描述

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


查看完整回答
反對 回復 2023-04-11
  • 1 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號