Home > Blockchain >  Turn multi-indexed series with different lengths and non-unique indexes into Dataframe
Turn multi-indexed series with different lengths and non-unique indexes into Dataframe

Time:10-14

0  6    1689.306931
   6     345.198020
   6     226.217822
   6      34.574257
   6      14.000000
           ...     
3  6       1.077353
   6       1.116176
   6       1.078431
   6       1.049020
   6       0.980294

Here's my multi-indexed series my_df, where I can access each same-index with my_df.loc[0]

6    1689.306931
6     345.198020
6     226.217822
6      34.574257
6      14.000000
6      63.683168
6      60.158416
6      60.198020
6      18.811881
6      22.316832

dtype: float64

Considering that each series isn't exactly the same length, how can I turn this multi-indexed series into a dataframe w/o throwing the error:

ValueError: cannot reindex from a duplicate axis

pd.unstack() throws:

ValueError: Index contains duplicate entries, cannot reshape

CodePudding user response:

Try enumerate the rows within one level with groupby.cumcount, then unstack:

# your first level seem to be identical, just drop it
(df.reset_index(level=1)           
   .set_index(df.groupby(level=0).cumcount(), append=True)
   .unstack()
)
  • Related