简体   繁体   中英

How to transform recurrent time series pandas data frame to pandas multi-index data frame

Is there a pandas function to transform the data frame represented below to multi-index time series data frame ?

    ticker  date    lastupdated ev  evebit  evebitda    marketcap   pb  pe  ps
None                                        
0   XOM  2018-12-31 2018-12-31  323071.3    12.3    7.1  288703.3   1.5 12.4    1.1
1   XOM  2018-12-28 2018-12-28  322986.6    12.3    7.1  288618.6   1.5 12.4    1.1
2   XOM  2018-12-27 2018-12-27  326246.7    12.5    7.1  291878.7   1.5 12.6    1.1
3   XOM  2018-12-26 2018-12-26  324976.5    12.4    7.1  290608.5   1.5 12.5    1.1
4   XOM  2018-12-24 2018-12-24  311724.7    11.9    6.8  277356.7   1.5 11.9    1.0
5   AAPL 2018-12-31 2018-10-21  1137146.7   16.2    14.0 1054517.7  9.2 18.8    4.1
6   AAPL 2018-12-28 2018-10-21  1151491.6   16.4    14.2 1068862.6  9.3 19.0    4.2
7   AAPL 2018-12-27 2018-10-21  1160185.5   16.5    14.3 1077556.5  9.4 19.2    4.2
8   AAPL 2018-12-26 2018-10-21  1178394.3   16.7    14.5 1095765.3  9.5 19.5    4.3
9   AAPL 2018-12-24 2018-10-21  1185590.9   16.8    14.6 1102961.9  9.6 19.7    4.3

to get the following data frame with regrouped date:

                 lastupdated    ev  evebit  evebitda    marketcap   pb  pe  ps
date      ticker                                            
2018-12-31 XOM    2018-12-31    323071.3    12.3    7.1  288703.3   1.5 12.4    1.1
           AAPL   2018-12-31    322986.6    12.3    7.1  288618.6   1.5 12.4    1.1
2018-12-28 XOM    2018-12-28    326246.7    12.5    7.1  291878.7   1.5 12.6    1.1
           AAPL   2018-12-28    324976.5    12.4    7.1  290608.5   1.5 12.5    1.1
2018-12-27 XOM    2018-12-27    311724.7    11.9    6.8  277356.7   1.5 11.9    1.0
           AAPL   2018-10-27    1137146.7   16.2    14.0 1054517.7  9.2 18.8    4.1
2018-12-26 XOM    2018-10-26    1151491.6   16.4    14.2 1068862.6  9.3 19.0    4.2
           AAPL   2018-10-26    1160185.5   16.5    14.3 1077556.5  9.4 19.2    4.2
2018-12-24 XOM    2018-10-24    1178394.3   16.7    14.5 1095765.3  9.5 19.5    4.3
           AAPL   2018-10-24    1185590.9   16.8    14.6 1102961.9  9.6 19.7    4.3

DataFrame.set_indexDataFrame.sort_index DataFrame.set_index使用:

df1 = df.set_index(['date', 'ticker']).sort_index(level=[0,1], ascending=[True, False])

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