Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

python - dataframe: easiest way to unstack the years from datetimeindex to columns header?

I have this dataframe with daily timeseries

Date         ID       Country  Type           Value
2018-01-21  340               ...         -1.130000
...................................................

2020-12-22  340               ...         -1.550000
2020-12-23  340               ...         -1.430000
2020-12-24  340               ...         -0.800000
2020-12-25  340               ...          0.000000

I wish to convert it like this

Date         2018   2019   2020                              

01-21       -1.13   23.21   1.22                 
01-22        2         3      1.5
...................................
12-22                .     -1.55       
12-23                      -1.43        
  1. is there a direct way or I have to first have 2 separate columns: year and month+ day before df.unstack()?

  2. actually the ts are from different countries and categories and I need to group by these 2 criteria, is it feasible to keep all in one df and use hierarchical index or I have to split the df and save them to a dictionary of dfs? (one country and category per df in the dict)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...