Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas melt() function, change the DataFrame format from wide to long

# pandas melt() function, change the DataFrame format from wide to long
import pandas as pd
df = pd.DataFrame({'Name': ['Bob', 'John', 'Foo', 'Bar', 'Alex', 'Tom'], 
                   'Math': ['A+', 'B', 'A', 'F', 'D', 'C'], 
                   'English': ['C', 'B', 'B', 'A+', 'F', 'A'],
                   'Age': [13, 16, 16, 15, 15, 13]})
df
  
#Use Melt
df1 = df.melt(id_vars=['Name', 'Age'], var_name='Subject', value_name='Grades')
df1
print(df1)
Comment

PREVIOUS NEXT
Code Example
Python :: python exception 
Python :: strip in python 
Python :: how to take multiple line inputs in python 
Python :: install local package python 
Python :: how to create an array in python 
Python :: Python from...import statement 
Python :: tweepy aut code 
Python :: python compare sets 
Python :: infinite while python 
Python :: making gifs via python 
Python :: alpha vantage import 
Python :: not equal to in django filter 
Python :: install anaconda python 2.7 and 3.6 
Python :: rotate matrix python 
Python :: python pip Failed to build cryptography 
Python :: mode with group by in python 
Python :: how to repeat if statement in python 
Python :: sum two columns pandas 
Python :: how to select rows with specific values in pandas 
Python :: python red table from pdf 
Python :: remove leading and lagging spaces dataframe python 
Python :: Program to find GCD or HCF of two numbers python 
Python :: extract text from pdf python 
Python :: python using random module 
Python :: how to get circumference from radius 
Python :: python logging 
Python :: cumulative frequency for python dataframe 
Python :: letters to numbers python 
Python :: bounding box python 
Python :: python argv 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =