0
0
Pandasdata~30 mins

Why reshaping data matters in Pandas - See It in Action

Choose your learning style9 modes available
Why reshaping data matters
📖 Scenario: Imagine you work in a small bakery. You have sales data for different types of bread sold each day. The data is messy and hard to analyze because it is not organized well. You want to reshape the data to make it easier to understand and find trends.
🎯 Goal: You will reshape the bakery sales data from a wide format to a long format using pandas. This will help you see daily sales for each bread type clearly.
📋 What You'll Learn
Create a pandas DataFrame with daily sales data for three bread types.
Create a variable to hold the list of bread types.
Use pandas melt function to reshape the data from wide to long format.
Print the reshaped DataFrame.
💡 Why This Matters
🌍 Real World
Reshaping data is important in real life because data often comes in formats that are hard to analyze. Changing the shape helps us see patterns and make better decisions.
💼 Career
Data scientists and analysts frequently reshape data to prepare it for analysis, reporting, and visualization. Knowing how to reshape data with pandas is a key skill in many data jobs.
Progress0 / 4 steps
1
Create the bakery sales DataFrame
Create a pandas DataFrame called sales_data with these exact columns and values:
'Day' with values ['Monday', 'Tuesday', 'Wednesday'],
'Sourdough' with values [10, 12, 9],
'Baguette' with values [5, 7, 6],
and 'Rye' with values [3, 4, 2].
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.

2
Create a list of bread types
Create a list called bread_types that contains the strings 'Sourdough', 'Baguette', and 'Rye'.
Pandas
Need a hint?

Use square brackets to create a list with the exact bread names as strings.

3
Reshape the data using pandas melt
Use the pandas melt function on sales_data to reshape it from wide to long format. Store the result in a variable called reshaped_data. Use id_vars='Day' and value_vars=bread_types. Set var_name='Bread' and value_name='Sales'.
Pandas
Need a hint?

Use pd.melt() with the correct parameters to reshape the DataFrame.

4
Print the reshaped DataFrame
Print the variable reshaped_data to display the reshaped bakery sales data.
Pandas
Need a hint?

Use print(reshaped_data) to show the reshaped data.