October 2018
Beginner to intermediate
676 pages
18h 30m
English
The input data in the current form is called wide form, as each item is represented in a column, increasing the width of the table. We need to create another dataset that is of a long format, where all the items are merged into one column, increasing the number of rows or the depth of the table.
The following is the code to achieve this:
# Create a long form DataFrame from wide form DFlong_sales = pd.melt(snacks_sales, var_name='Item', value_name='Sales', id_vars=['daywk', 'weekend', 'Date', 'Promotion', 'Period', 'Month', 'Quarter'])long_sales.shape # dimensions, number of rows and columnslong_sales.sample(n=5) # Display random 5 sample rows
Here is how five random rows in the dataset look:
Read now
Unlock full access