📜  Python| Pandas.melt()

📅  最后修改于: 2022-05-13 01:54:42.553000             🧑  作者: Mango

Python| Pandas.melt()

为了更容易分析表格中的数据,我们可以使用Python中的 Pandas 将数据重塑为对计算机更友好的形式。 Pandas.melt() 是这样做的函数之一。
Pandas.melt() 将 DataFrame 从宽格式转为长格式。
melt()函数可用于将 DataFrame 消息传递为一种格式,其中一列或多列是标识符变量,而所有其他列,被认为是测量变量,都不会旋转到行轴,只留下两个非标识符列,变量和值。
句法 :

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

参数:

例子:

Python3
# Create a simple dataframe
  
# importing pandas as pd
import pandas as pd
  
# creating a dataframe
df = pd.DataFrame({'Name': {0: 'John', 1: 'Bob', 2: 'Shiela'},
                   'Course': {0: 'Masters', 1: 'Graduate', 2: 'Graduate'},
                   'Age': {0: 27, 1: 23, 2: 21}})
df


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


Python3
# multiple unpivot columns
pd.melt(df, id_vars =['Name'], value_vars =['Course', 'Age'])


Python3
# Names of ‘variable’ and ‘value’ columns can be customized
pd.melt(df, id_vars =['Name'], value_vars =['Course'],
              var_name ='ChangedVarname', value_name ='ChangedValname')



Python3

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


Python3

# multiple unpivot columns
pd.melt(df, id_vars =['Name'], value_vars =['Course', 'Age'])



Python3

# Names of ‘variable’ and ‘value’ columns can be customized
pd.melt(df, id_vars =['Name'], value_vars =['Course'],
              var_name ='ChangedVarname', value_name ='ChangedValname')