Search code examples
pythonpandasdataframemulti-index

Transform a Pandas dataframe in a pandas with multicolumns


I have the following pandas dataframe, where the column id is the dataframe index

+----+-----------+------------+-----------+------------+
|    |   price_A |   amount_A |   price_B |   amount_b |
|----+-----------+------------+-----------+------------|
|  0 | 0.652826  |  0.941421  |  0.823048 |  0.728427  |
|  1 | 0.400078  |  0.600585  |  0.194912 |  0.269842  |
|  2 | 0.223524  |  0.146675  |  0.375459 |  0.177165  |
|  3 | 0.330626  |  0.214981  |  0.389855 |  0.541666  |
|  4 | 0.578132  |  0.30478   |  0.789573 |  0.268851  |
|  5 | 0.0943601 |  0.514878  |  0.419333 |  0.0170096 |
|  6 | 0.279122  |  0.401132  |  0.722363 |  0.337094  |
|  7 | 0.444977  |  0.333254  |  0.643878 |  0.371528  |
|  8 | 0.724673  |  0.0632807 |  0.345225 |  0.935403  |
|  9 | 0.905482  |  0.8465    |  0.585653 |  0.364495  |
+----+-----------+------------+-----------+------------+

And I want to convert this dataframe in to a multi column data frame, that looks like this


+----+-----------+------------+-----------+------------+
|    |           A            |           B            |
+----+-----------+------------+-----------+------------+
| id |   price   |   amount   |   price   |   amount   |
|----+-----------+------------+-----------+------------|
|  0 | 0.652826  |  0.941421  |  0.823048 |  0.728427  |
|  1 | 0.400078  |  0.600585  |  0.194912 |  0.269842  |
|  2 | 0.223524  |  0.146675  |  0.375459 |  0.177165  |
|  3 | 0.330626  |  0.214981  |  0.389855 |  0.541666  |
|  4 | 0.578132  |  0.30478   |  0.789573 |  0.268851  |
|  5 | 0.0943601 |  0.514878  |  0.419333 |  0.0170096 |
|  6 | 0.279122  |  0.401132  |  0.722363 |  0.337094  |
|  7 | 0.444977  |  0.333254  |  0.643878 |  0.371528  |
|  8 | 0.724673  |  0.0632807 |  0.345225 |  0.935403  |
|  9 | 0.905482  |  0.8465    |  0.585653 |  0.364495  |
+----+-----------+------------+-----------+------------+

I've tried transforming my old pandas dataframe in to a dict this way:

   dict = {"A": df[["price_a","amount_a"]], "B":df[["price_b", "amount_b"]]}
   df = pd.DataFrame(dict, index=df.index)

But I had no success, how can I do that?


Solution

  • Try renaming columns manually:

    df.columns=pd.MultiIndex.from_tuples([x.split('_')[::-1] for x in df.columns])
    df.index.name='id'
    

    Output:

               A                   B         b
           price    amount     price    amount
    id                                        
    0   0.652826  0.941421  0.823048  0.728427
    1   0.400078  0.600585  0.194912  0.269842
    2   0.223524  0.146675  0.375459  0.177165
    3   0.330626  0.214981  0.389855  0.541666
    4   0.578132  0.304780  0.789573  0.268851
    5   0.094360  0.514878  0.419333  0.017010
    6   0.279122  0.401132  0.722363  0.337094
    7   0.444977  0.333254  0.643878  0.371528
    8   0.724673  0.063281  0.345225  0.935403
    9   0.905482  0.846500  0.585653  0.364495