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')
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)
)