July 2019
Beginner to intermediate
740 pages
16h 52m
English
In order to go from wide format to long format, we need to melt the data. Melting undoes a pivot. DataFrame objects have a melt() method for flexible reshaping. There is also a wrapper function called pd.wide_to_long() that uses the melt() method, but is less flexible.
For this example, we need to read in the wide format of this data from the data/wide_data.csv file:
>>> wide_df = pd.read_csv('data/wide_data.csv')>>> wide_df.head()
Our wide data contains a column for the date and a column for each temperature measurement we have been working with:
| date | TMAX | TMIN | TOBS | |
|---|---|---|---|---|
| 0 | 2018-10-01 | 21.1 | 8.9 | 13.9 |
| 1 | 2018-10-02 | 23.9 | 13.9 | 17.2 |
| 2 | 2018-10-03 | 25.0 | 15.6 | 16.1 |
| 3 | 2018-10-04 | 22.8 | 11.7 | 11.7 |
| 4 | 2018-10-05 | 23.3 | 11.7 | 18.9 ... |
Read now
Unlock full access