1

I am trying to do pivot of a two categorical Variable (status_type) in python pandas but it is resulting in multiindex dataframe. I would like this to be a normal dataframe but couldn't figure out how. Would appreciate any help.

import pandas as pd
import numpy as np

# data
test_df = pd.DataFrame({"id": np.arange(0, 8), 
                   "Window": np.random.rand(8), 
                   "status_type" : ["snap","perf","snap","perf","snap","perf","snap","perf"],
                   "status_level": [1, 2, 20, 35, 10, 5, 42, 9],
                  })

test_df

Pivot:

test_df.pivot(index=['id','Window'],columns='status_type',values='status_level')

Result in Multi index df: result

I have tried below code to reset it into normal dataframe and remove status_type column but it didn't work.

(test_df
.pivot(index=['id','Window'], columns='status_type', values='status_level')
.reset_index()
.drop('status_type', axis = 1)
)
ViSa
  • 1,563
  • 8
  • 30

2 Answers2

1

A possible solution:

(test_df.pivot(index=['id','Window'],columns='status_type',values='status_level')
 .rename_axis(None, axis=1).reset_index())

Output:

   id    Window  perf  snap
0   0  0.218674   NaN   1.0
1   1  0.744127   2.0   NaN
2   2  0.888882   NaN  20.0
3   3  0.399161  35.0   NaN
4   4  0.955325   NaN  10.0
5   5  0.938839   5.0   NaN
6   6  0.874510   NaN  42.0
7   7  0.964414   9.0   NaN
PaulS
  • 21,159
  • 2
  • 9
  • 26
  • 1
    Yes this looks good and exactly what I needed. Thanks. Will accept it as soon as it let me. – ViSa Jul 25 '23 at 10:43
1

This will help you resolve using reset and iloc ,and previous solution is more efficient as we are doing within the pivot function thanks to Paul

This will help you resolve using reset and iloc ,and previous solution is more efficient as we are doing within the pivot function thanks to Paul