Reshaping using melt

The melt function converts data into a wide format to a single column consisting of unique ID-variable combinations.

The R melt() function

Here, we demonstrate the use of the melt() function in R. It produces long-format data in which the rows are unique variable-value combinations:

>sample4=head(flights.sample,4)[c('year','month','dep_delay','arr_delay')]
> sample4
         year month dep_delay arr_delay
  155501 2013     3          2         5
  2410   2013     1          0         4
  64158  2013    11         -7       -27
  221447 2013     5         -5       -12

>melt(sample4,id=c('year','month'))
         year month  variable value
       1 2013     3 dep_delay     2
       2 2013     1 dep_delay     0
       3 2013    11 dep_delay    -7
       4 2013     5 dep_delay    -5
       5 2013     3 arr_delay     5
       6 2013     1 arr_delay     4
       7 2013    11 arr_delay   -27
       8 2013     5 arr_delay   -12
>

For more information, you can refer to the following: http://www.statmethods.net/management/reshape.html.

The pandas melt() function

In pandas, the melt function is similar:

In [55]: sample_4_df=flights_sample_df[['year','month','dep_delay', 
'arr_delay']].head(4)
In [56]: sample_4_df
Out[56]:    year   month dep_delay arr_delay
        0   2013   3       2       5
        1   2013   1       0       4
        2   2013   11      -7      -27
        3   2013   5       -5      -12

In [59]: pd.melt(sample_4_df,id_vars=['year','month'])
Out[59]: year   month   variable        value
        0   2013   3       dep_delay        2
        1   2013   1       dep_delay        0
        2   2013   11      dep_delay       -7
        3   2013   5       dep_delay       -5
        4   2013   3       arr_delay        5
        5   2013   1       arr_delay        4
        6   2013   11      arr_delay       -27
        7   2013   5       arr_delay       -12

The reference for this information is from: http://pandas.pydata.org/pandas-docs/stable/reshaping.html#reshaping-by-melt.

..................Content has been hidden....................

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