简体   繁体   中英

How to convert a pd.Series to a pd.DataFrame where indices are dates and columns are time?

I have pd.Series whose index are pd.DataTimeIndex. I would like to convert the 1D series to a DataFrame whose indices are the dates and columns are time of each data. Something like this:

            08:01:00  08:02:00  08:03:00  08:04:00  08:05:00
2011-04-04      2.50      2.65      2.65       2.7       2.8
2011-04-05     -4.30     -4.45     -4.70      -4.6      -5.0
2011-04-06     25.75     26.30     26.50      26.7      27.1
2011-04-07      1.15     -0.45     -0.45      -0.4       0.2

Let us do

s.index=pd.MultiIndex.from_arrays([s.index.date,s.index.time])
df=s.unstack()
df
            08:01:00  08:02:00  08:03:00  08:04:00  08:05:00
2011-04-04      2.50      2.65      2.65       2.7       2.8
2011-04-05     -4.30     -4.45     -4.70      -4.6      -5.0
2011-04-06     25.75     26.30     26.50      26.7      27.1
2011-04-07      1.15     -0.45     -0.45      -0.4       0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM