'How to unmelt a completely melted table
I have this dataframe df
which I have melted and then using pd.pivot_table I am able to get the table structure back at least looking at the rows it seems so - but the indexes become MultiIndex
type - is there a way to change to back to RangeIndex
as it was in the original df
Here are the steps
>>> import pandas as pd
>>> df = pd.DataFrame({'Name': ['Bob', 'John', 'Foo', 'Bar', 'Alex', 'Tom'],
'Math': ['A+', 'B', 'A', 'F', 'D', 'C'],
'Mental Math': ['A', 'A', 'B', 'C', 'E', 'B'],
'English': ['C', 'B', 'B', 'A+', 'F', 'A'],
'Bengali': ['C', 'C', 'A', 'B', 'A+', 'B'],
'Age': [13, 16, 16, 15, 15, 13]})
>>> print(df)
Name Math Mental Math English Bengali Age
0 Bob A+ A C C 13
1 John B A B C 16
2 Foo A B B A 16
3 Bar F C A+ B 15
4 Alex D E F A+ 15
5 Tom C B A B 13
>>> d = df.melt(var_name='Column', value_name='Value')
>>> print(d)
Column Value
0 Name Bob
1 Name John
2 Name Foo
3 Name Bar
4 Name Alex
5 Name Tom
6 Math A+
7 Math B
8 Math A
9 Math F
10 Math D
11 Math C
12 Mental Math A
13 Mental Math A
14 Mental Math B
15 Mental Math C
16 Mental Math E
17 Mental Math B
18 English C
19 English B
20 English B
21 English A+
22 English F
23 English A
24 Bengali C
25 Bengali C
26 Bengali A
27 Bengali B
28 Bengali A+
29 Bengali B
30 Age 13
31 Age 16
32 Age 16
33 Age 15
34 Age 15
35 Age 13
Is it possible to get back to the original table as in the original indexes
Using pandas.pivot_table
tried to return to the original dataframe but if you see the index now has become a MultiIndex type
>>> df_back = pd.pivot_table(data = d, index=['Name','Age'], columns=['Column'], aggfunc=''.join, margins=False)
>>> print(df_back)
Value
Column Bengali English Math Mental Math
Name Age
Alex 15 A+ F D E
Bar 15 B A+ F C
Bob 13 C C A+ A
Foo 16 A B A B
John 16 C B B A
Tom 13 B A C B
>>> print(df_back.index)
MultiIndex([('Alex', 15),
( 'Bar', 15),
( 'Bob', 13),
( 'Foo', 16),
('John', 16),
( 'Tom', 13)],
names=['Name', 'Age'])
>>> print(df.index)
RangeIndex(start=0, stop=6, step=1)
Solution 1:[1]
As @mozway said your code does not work, some steps must be missing
But to go from d to df you can use unstack
. You need a unique index for this to work hence this is where 'count'
column comes in
(d.assign(count = np.arange(len(d))%6)
.set_index(['count','Column'])
.unstack(level=1)
.reset_index(drop = True)
.droplevel(level = 0, axis=1)
)
output
Age Bengali English Math Mental Math Name
-- ----- --------- --------- ------ ------------- ------
0 13 C C A+ A Bob
1 16 C B B A John
2 16 A B A B Foo
3 15 B A+ F C Bar
4 15 A+ F D E Alex
5 13 B A C B Tom
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | piterbarg |