Home > other >  Rearranging a Dataframe into 1 column and 1 index
Rearranging a Dataframe into 1 column and 1 index

Time:06-04

I have the following dataframe and I want to create a new dataframe with one column being unique strikes (no repetition) and one index (TimeUntilExpiration-unique values) with the Mid values being the values that corresponds to a unique combination of TimeUntilExpiration and Strike.

 TimeUntilExpiration  Strike    Mid

0               0.010959   22.75  2.325
1               0.010959   23.00  2.200
2               0.010959   23.25  2.100
3               0.010959   23.50  1.975
4               0.010959   23.75  1.875
..                   ...     ...    ...
307             2.043836   25.00  1.455
308             2.043836   26.00  1.015
309             2.043836   27.00  2.795
310             2.043836   28.00  0.395
311             2.043836   29.00  0.205

Tried so many ways and mapping but nothing seemed to work. Appreciate any help:)

CodePudding user response:

Maybe you want to pivot your dataframe?

>>> df.pivot_table('Mid', 'TimeUntilExpiration', 'Strike')

Strike               22.75  23.00  23.25  23.50  23.75  25.00  26.00  27.00  28.00  29.00
TimeUntilExpiration                                                                      
0.010959             2.325    2.2    2.1  1.975  1.875    NaN    NaN    NaN    NaN    NaN
2.043836               NaN    NaN    NaN    NaN    NaN  1.455  1.015  2.795  0.395  0.205

CodePudding user response:

pd.melt(df, id_vars=["TimeUntilExpiration",'Strike'], value_vars=["Mid"], value_name='Mid values').drop('variable', axis=1)

Result:

index TimeUntilExpiration Strike Mid values
0 0.010959 22.75 2.325
1 0.010959 23.0 2.2
2 0.010959 23.25 2.1
3 0.010959 23.5 1.975
4 0.010959 23.75 1.875
5 2.043836 25.0 1.455
6 2.043836 26.0 1.015
7 2.043836 27.0 2.795
8 2.043836 28.0 0.395
9 2.043836 29.0 0.205
  • Related