将数据框重新排列为 1 列和 1 个索引

Rearranging a Dataframe into 1 column and 1 index

我有以下数据框,我想创建一个新的数据框,其中一列是唯一的罢工(无重复)和一个索引(TimeUntilExpiration-唯一值),中间值是对应于唯一组合的值TimeUntilExpiration 和罢工。

 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

尝试了很多方法和映射,但似乎没有任何效果。感谢任何帮助:)

也许你想

>>> 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
pd.melt(df, id_vars=["TimeUntilExpiration",'Strike'], value_vars=["Mid"], value_name='Mid values').drop('variable', axis=1)

结果:

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