TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Pandas melt()

Pandas melt() with What is Python Pandas, Reading Multiple Files, Null values, Multiple index, Application, Application Basics, Resampling, Plotting the data, Moving windows functions, Series, Read the file, Data operations, Filter Data etc.

<< Back to PANDAS

Pandas melt()

The Pandas.melt() function is used to unpivot the DataFrame from a wide format to a long format.

Its main task is to massage a DataFrame into a format where some columns are identifier variables and remaining columns are considered as measured variables, are unpivoted to the row axis. It leaves just two non-identifier columns, variable and value.

Syntax

pandas.melt(frame, id_vars=None, value_vars=None,
 var_name=None, value_name='value', col_level=None)

Parameters

  • frame: It refers to the DataFrame.
  • id_vars[tuple, list, or ndarray, optional]: It refers to the columns to use as identifier variables.
  • value_vars[tuple, list, or ndarray, optional]: Refers to columns to unpivot. If it is not specified, use all columns that are not set as id_vars.
  • var_name[scalar]: Refers to a name to use for the 'variable' column. If it is None, it uses frame.columns.name or 'variable'.
  • value_name[scalar, default 'value']: Refers to a name to use for the 'value' column.
  • col_level[int or string, optional]: It will use this level to melt if the columns are MultiIndex.

Returns

It returns the unpivoted DataFrame as the output.

Example

# importing pandas as pd 
import pandas as pd   
# creating a dataframe 
info = pd.DataFrame({'Name': {0: 'Parker', 1: 'Smith', 2: 'John'}, 
                   'Language': {0: 'Python', 1: 'Java', 2: 'C++'}, 
                   'Age': {0: 22, 1: 30, 2: 26}}) 

# Name is id_vars and Course is value_vars 
pd.melt(info, id_vars =['Name'], value_vars =['Language']) 
info

Output

      Name    Language     Age
0     Parker    Python     22
1     Smith     Java       30
2     John      C++        26

Example2

import pandas as pd 
info = pd.DataFrame({'A': {0: 'p', 1: 'q', 2: 'r'},
'B': {0: 40, 1: 55, 2: 25},
'C': {0: 56, 1: 62, 2: 42}})
pd.melt(info, id_vars=['A'], value_vars=['C'])
pd.melt(info, id_vars=['A'], value_vars=['B', 'C'])
pd.melt(info, id_vars=['A'], value_vars=['C'],
var_name='myVarname', value_name='myValname')

Output

       A    myVarname    myValname
0      p      C            56                
1      q      C            62                
2      r      C            42                

Next TopicDataFrame.merge()




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf