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)
参数:
frame : DataFrame
id_vars[tuple, list, or ndarray, optional] : Column(s) to use as identifier variables.
value_vars[tuple, list, or ndarray, optional]: Column(s) to unpivot. If not specified, uses all columns that are not set as id_vars.
var_name[scalar]: Name to use for the ‘variable’ column. If None it uses frame.columns.name or ‘variable’.
value_name[scalar, default ‘value’]: Name to use for the ‘value’ column.
col_level[int or string, optional]: If columns are a MultiIndex then use this level to melt.
例子:
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')