Pandas Melt and Pivot
Pandas melt() function is used to change the DataFrame format from wide to long. Long format is better suited for data analytics and machine learning.
pivot() and its general form pivot-table() do the oppisite which transform the data from a long to wide format. They are very similar to Microsoft Excel's pivot table and are used to summarize data.
Difference between pivot() and pivot-table:
- pivot() has not option to perform aggregate functions
- pivot-table() has an option to perform aggregate functions
Step 1 - Use Test Data
We follow this chart to demonstrate the usage of melt() and pivot(). The test data is tiny and does not look wide, however when all categories of a categorial variable are used as columns, the data will be very wide. We will see this clearly in step 2 and 3 with real COVID-19 datasets.
Step 2 - Use Data from ourworldindata.com
This dataset have each country as a column which makes the data "wide". We will melt it into a "long" data.
Step 3 - Use Data from Kaggle.com
This dataset have each day as a column which makes the data "wide". We will melt it into a "long" data.