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

image.png
例子:

  1. >>> df
  2. ...
  3. ...
  4. >>>df
  5. Country Name 1990 2015
  6. 40 China 73.558 44.500
  7. 55 Germany 26.882 22.800
  8. 119 Japan 22.661 8.619
  9. 251 United States 24.700 18.329
  1. # melt year columns and convert year to date time
  2. df_melt = df.melt(id_vars='Country Name', value_vars = ['1990', '2015'])
  3. df_melt.columns = ['country','year', 'variable']
  4. df_melt['year'] = df_melt['year'].astype('datetime64[ns]').dt.year
  1. >>> df_melt
  2. country year variable
  3. 0 China 1990 73.558
  4. 1 Germany 1990 26.882
  5. 2 Japan 1990 22.661
  6. 3 United States 1990 24.700
  7. 4 China 2015 44.500
  8. 5 Germany 2015 22.800
  9. 6 Japan 2015 8.619
  10. 7 United States 2015 18.329