0

I have a df like this

| Feature | Channel | Value |
|---------|---------|-------|
| a       | 0       | 1.3   |
|         | 1       | 1.3   |
|         | 2       | 1.3   |
| b       | 0       | 1.2   |
|         | 1       | 1.2   |
|         | 2       | 1.2   |

where Feature and Channel are a multiindex

how can I turn this into the following:

| channel | a   | b   |
|---------|-----|-----|
| 0       | 1.3 | 1.2 |
| 1       | 1.3 | 1.2 |
| 2       | 1.3 | 1.2 |

where channel is now the index, and a and b are separate columns?

0

2 Answers 2

2

Use unstack:

new_df = df.unstack(level=0)

new_df:

        Value     
Feature     a    b
Channel           
0         1.3  1.2
1         1.3  1.2
2         1.3  1.2

With droplevel + rename_axis:

new_df = df.unstack(0).droplevel(0, 1).rename_axis(columns=None)

new_df:

           a    b
Channel          
0        1.3  1.2
1        1.3  1.2
2        1.3  1.2
Sign up to request clarification or add additional context in comments.

Comments

0

Pivot might be easier (drop the index first) -

df.reset_index(inplace=True)
df.pivot(index='Channel',columns='Feature',values='Value')

1 Comment

Yep, you beat me to my edit, revised to show reset_index first.

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.