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

0

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)
Posted by: Guest on March-25-2022

Code answers related to "pandas melt() function, change the DataFrame format from wide to long"

Python Answers by Framework

Browse Popular Code Answers by Language