2

I have a set of experiments defined by two variables: scenario and height. For each experiment, I take 3 measurements: result 1, 2 and 3. The dataframe that collects all the results looks like this:

import numpy as np
import pandas as pd

df = pd.DataFrame()
df['Scenario']= np.repeat(['Scenario a','Scenario b','Scenario c'],3)
df['height'] = np.tile([0,1,2],3)
df['Result 1'] = np.arange(1,10)
df['Result 2'] = np.arange(20,29)
df['Result 3'] = np.arange(30,39)

If I run the following:

mypiv = df.pivot('Scenario','height').transpose()
writer = pd.ExcelWriter('test_df_pivot.xlsx')
mypiv.to_excel(writer,'test df pivot')
writer.save()

I obtain a dataframe where columns are the scenarios, and the rows have a multi-index defined by result and height:

+----------+--------+------------+------------+------------+
|          | height | Scenario a | Scenario b | Scenario c |
+----------+--------+------------+------------+------------+
| Result 1 |      0 |          1 |          4 |          7 |
|          |      1 |          2 |          5 |          8 |
|          |      2 |          3 |          6 |          9 |
| Result 2 |      0 |         20 |         23 |         26 |
|          |      1 |         21 |         24 |         27 |
|          |      2 |         22 |         25 |         28 |
| Result 3 |      0 |         30 |         33 |         36 |
|          |      1 |         31 |         34 |         37 |
|          |      2 |         32 |         35 |         38 |
+----------+--------+------------+------------+------------+

How can I create a pivot where the indices are swapped, i.e. height first, then result?

I couldn't find a way to create it directly. I managed to get what I want swapping the levels and the re-sorting the results:

mypiv2 = mypiv.swaplevel(0,1 , axis=0).sortlevel(level=0,axis=0,sort_remaining=True)

but I was wondering if there is a more direct way.

1 Answer 1

1

You can first set_index and then stack with unstack:

print (df.set_index(['height','Scenario']).stack().unstack(level=1))
Scenario         Scenario a  Scenario b  Scenario c
height                                             
0      Result 1           1           4           7
       Result 2          20          23          26
       Result 3          30          33          36
1      Result 1           2           5           8
       Result 2          21          24          27
       Result 3          31          34          37
2      Result 1           3           6           9
       Result 2          22          25          28
       Result 3          32          35          38
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.