Python Forum
How to melt dataframe multiple columns to one column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to melt dataframe multiple columns to one column
#1
Hi,
I have below dataframe, I want to melt all columns to one column. I use below code, but some thing I am missing


import pandas as pd
df = pd.DataFrame({'ID':['A1','A2','A1'],\
'Estart':[1,2,3],\
'Eend':[4,4,5],\
'Rstart':[1,2,1],\
'Rend':[1,6,5]})
pd.melt(df, id_vars=['ID'], value_vars=['Estart','Eend','Rstart','Rend'])
Igot the below outputL
	ID	variable	value
0	A1	Estart	1
1	A2	Estart	4
2	A1	Estart	3
3	A1	Eend	4
4	A2	Eend	4
5	A1	Eend	5
6	A1	Rstart	1
7	A2	Rstart	2
8	A1	Rstart	1
9	A1	Rend	1
10	A2	Rend	6
11	A1	Rend	5
I want as below: Some help how to arrange ad below:


ID	variable	value
A1	Estart	1
A1	Eend	4
A1	Rstart	1
A1	Rend	1
A2	Estart	2
A2	Eend	4
A2	Rstart	2
A2	Rend	6
A1	Estart	3
A1	Eend	5
A1	Rstart	1
A1	Rend	5
Reply


Messages In This Thread
How to melt dataframe multiple columns to one column - by Mekala - Sep-23-2020, 10:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 248 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  Nested for loops: Iterating over columns of a DataFrame to plot on subplots dm222 0 286 Aug-19-2022, 11:07 AM
Last Post: dm222
  renaming the 0 column in a dataframe Led_Zeppelin 5 467 Aug-16-2022, 04:07 PM
Last Post: deanhystad
  How to combine multiple column values into 1? cubangt 15 624 Aug-11-2022, 08:25 PM
Last Post: cubangt
  Copy a column from one dataframe to another dataframe Led_Zeppelin 17 1,789 Jul-08-2022, 08:40 PM
Last Post: deanhystad
  How to move multiple columns to initial position SriRajesh 4 576 Jul-02-2022, 10:34 AM
Last Post: deanhystad
  Reshaping a single column in to multiple column using Python sahar 7 822 Jun-20-2022, 12:35 PM
Last Post: deanhystad
  df column aggregate and group by multiple columns SriRajesh 0 459 May-06-2022, 02:26 PM
Last Post: SriRajesh
  Extract parts of multiple log-files and put it in a dataframe hasiro 4 793 Apr-27-2022, 12:44 PM
Last Post: hasiro
  Transform 3 Columns into Single Column DaveG 8 763 Apr-04-2022, 08:42 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020