I have a pandas.Dataframe
with the following columns:
a_1 ab_1 ac_1 a_2 ab_2 ac_2
2 3 4 5 6 7
How do I convert it into the following?
a ab ac
2 3 4
5 6 7
I was trying to use pandas melt to convert from wide to long format, but not sure of the syntax.
You can replace the columns by a multi-index and stack:
df.columns = pd.MultiIndex.from_tuples(df.columns.str.split('_').map(tuple))
df = df.stack()
Here is one way to do that:
df.columns = pd.MultiIndex.from_tuples(
[c.split('_') for c in df.columns], names=['col', 'row'])
df.melt().pivot(index='row', columns='col', values='value')
Create a pandas.MultiIndex
for the columns by splitting on _
.
melt
the data frame and then pivot
on the elements from the original column names.
df = pd.DataFrame(
data=[range(2, 8)],
columns='a_1 ab_1 ac_1 a_2 ab_2 ac_2'.split()
)
print(df)
df.columns = pd.MultiIndex.from_tuples(
[c.split('_') for c in df.columns], names=['col', 'row'])
print(df.melt().pivot(index='row', columns='col', values='value'))
a_1 ab_1 ac_1 a_2 ab_2 ac_2
0 2 3 4 5 6 7
col a ab ac
row
1 2 3 4
2 5 6 7
If using pandas prior to 0.20.0, melt()
like:
print(pd.melt(df).pivot(index='row', columns='col', values='value'))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With