I have a pandas Series where the index is a StructType. I want to explode this index into the structfields.
index | value |
---|---|
(2005-07-03, beta0) | 0.997160 |
(2005-07-03, beta1) | 0.037629 |
(2005-07-03, beta2) | 0.037629 |
(2005-07-03, tau) | 2.000000 |
(2011-11-13, beta0) | 0.997160 |
Here the index is of the structype key: struct<checkin_week:date,parameter:string>
I need to explode it two columns such that I have a column for checkin_week and another column for parameter.
The following will give you a DataFrame
with a MultiIndex
, in which checkin_week
and parameter
will be separate levels.
import pandas as pd
df = pd.DataFrame(
data=(
0.997160,
0.037629,
0.037629,
2.000000,
0.997160,
),
index=(
("2005-07-03", "beta0"),
("2005-07-03", "beta1"),
("2005-07-03", "beta2"),
("2005-07-03", "tau"),
("2011-11-13", "beta0"),
),
)
df.index = pd.MultiIndex.from_tuples(df.index)
If you want them to be separate columns (instead of indexes), you should first reset your DataFrame
's index:
df = df.reset_index(drop=False)
And then follow the answers of this question