Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 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)

question from:https://stackoverflow.com/questions/65599141/dataframe-easiest-way-to-unstack-the-years-from-datetimeindex-to-columns-header

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...