Skip to content

Latest commit

 

History

History
40 lines (28 loc) · 1.12 KB

73q4.md

File metadata and controls

40 lines (28 loc) · 1.12 KB

lang.py.module.pandas.index.melting

Tabularize columnar index data into row data

Synopsis

  df.melt()

Overview

Sometimes you may have some columnar data that you want to transpose into row data, for that you can use melt to extact that columnar data from a dataframe

  • It will repeat values from your column labels to match all it's row records

Some of it's arguments are as follow:

  • value_vars: An array like of column names to melt (mandatory)
  • ignore_index: Ignore the original index of the dataframe (default True)
  • var_name: Name to give to the melted column
  • id_vars: Array like of columns from original dataframe to keep and repeat values

Cookbook

Take a column and turn it into a rows

Unlike stacking this will not turn your column into part of the index, it will create it as a new column where it's values match the corresponing value from the orginal dataframe

  df.melt(
      id_vars=['sector'],
      value_vars=df.columns.difference['sector']
      var_name='date'
  )