I have a dataframe similar to
df = pd.DataFrame(
pd.np.random.randint(1, 10, (20, 2)),
index=["a", "b", "c", "d"] * 5,
columns=["foo", "bar"],
)
looking something like this
I'm trying to get it into the following multi-index form: two columns for each of the 4 axis labels each with a "foo" and "bar" sub-column:
I've tried fiddling around with df.groupby(df.index)
but then I can't figure out how to turn the groups into columns.
Use GroupBy.cumcount
for MultiIndex
in index with DataFrame.set_index
, then reshape by DataFrame.unstack
and change order of Multiindex
in columns by DataFrame.swaplevel
with DataFrame.sort_index
:
df1 = (df.set_index(df.groupby(level=0).cumcount(), append=True)
.unstack(0)
.swaplevel(0,1, axis=1)
.sort_index(axis=1, level=[0,1], ascending=[True, False]))
print (df1)
a b c d
foo bar foo bar foo bar foo bar
0 6 8 1 6 8 4 6 1
1 2 6 4 5 3 1 5 4
2 2 5 2 9 5 4 8 9
3 5 2 6 6 7 3 2 9
4 4 8 9 6 4 2 1 5
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.