How it works...

The melt method is powerful and dramatically reshapes your DataFrame. It takes up to five parameters, with two of them being crucial to understanding how to reshape your data correctly:

  • id_vars is a list of column names that you want to preserve as columns and not reshape
  • value_vars is a list of column names that you want to reshape into a single column

The id_vars, or the identification variables, remain in the same column but repeat for each of the columns passed to value_vars. One crucial aspect of melt is that it ignores values in the index, and, in fact, it silently drops your index and replaces it with a default RangeIndex. This means that if you do have values in your index that you would like to keep, you will need to reset the index first before using melt.

It is somewhat common terminology to refer to the transformation of horizontal column names into vertical column values as melting, stacking, or unpivoting.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.21.104.183