df.melt
pandas.melt(frame, id_vars=None, value_vars=None, var_name=None, value_name=’value’, col_level=None, ignore_index=True)[source]
Unpivot a DataFrame from wide to long format, optionally leaving identifiers set.
This function is useful to massage a DataFrame into a format where one or more columns are identifier variables (idvars), while all other columns, considered measured variables (value_vars), are “unpivoted” to the row axis, leaving just two non-identifier columns, ‘variable’ and ‘value’.
Parameters
**id_vars_tuple, list, or ndarray, optional
Column(s) to use as identifier variables.
valuevars_tuple, list, or ndarray, optional
Column(s) to unpivot. If not specified, uses all columns that are not set as id_vars.
varname_scalar
Name to use for the ‘variable’ column. If None it uses frame.columns.name or ‘variable’.
valuename_scalar, default ‘value’
Name to use for the ‘value’ column.
collevel_int or str, optional
If columns are a MultiIndex then use this level to melt.
ignoreindex_bool, default True**
If True, original index is ignored. If False, the original index is retained. Index labels will be repeated as necessary.
Returns: DataFrame
例子:
>>> df
...
...
>>>df
Country Name 1990 2015
40 China 73.558 44.500
55 Germany 26.882 22.800
119 Japan 22.661 8.619
251 United States 24.700 18.329
# melt year columns and convert year to date time
df_melt = df.melt(id_vars='Country Name', value_vars = ['1990', '2015'])
df_melt.columns = ['country','year', 'variable']
df_melt['year'] = df_melt['year'].astype('datetime64[ns]').dt.year
>>> df_melt
country year variable
0 China 1990 73.558
1 Germany 1990 26.882
2 Japan 1990 22.661
3 United States 1990 24.700
4 China 2015 44.500
5 Germany 2015 22.800
6 Japan 2015 8.619
7 United States 2015 18.329