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

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

將多索引轉換為單個日期

將多索引轉換為單個日期

LEATH 2023-07-11 13:53:45
我有一個數據框,其中索引是多重索引,如下所示;     1     2     3     4     52020 6     7     8     9     10     11     12由于我需要每日值,我希望將其轉換為如下列;Date2020/01/012020/02/012020/03/012020/04/012020/05/01etc 這樣我就可以使用;df.set_index('Date').resample('D').ffill()非常感謝任何幫助!
查看完整描述

1 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

如果索引中只有year并且month級別使用列表理解并更改連接字符串的格式:


#python 3

df.index = pd.to_datetime([f'{a}--01' for a, b in df.index])


#python 2

df.index = pd.to_datetime(['{}-{}-01'.format(a, b) for a, b in df.index])

如果還有天:


#python 3

df.index = pd.to_datetime([f'{a}--{c}' for a, b, c in df.index])


#python 2

df.index = pd.to_datetime(['{}-{}-{}'.format(a, b, c) for a, b, c in df.index])

進而:


df1 = df.resample('D').ffill()


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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