0
0
Data Analysis Pythondata~30 mins

Reshaping and transposing in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Reshaping and transposing
📖 Scenario: You work in a small bakery that tracks daily sales of three types of bread. The sales data is stored in a simple table format. You want to reshape this data to better analyze it and then transpose it to switch rows and columns for a different view.
🎯 Goal: Learn how to reshape a data table from wide to long format and then transpose it using Python's pandas library.
📋 What You'll Learn
Create a pandas DataFrame with specific sales data
Create a variable to hold the list of bread types
Use pandas melt function to reshape the DataFrame
Transpose the reshaped DataFrame
Print the final transposed DataFrame
💡 Why This Matters
🌍 Real World
Reshaping and transposing data is common when preparing sales or survey data for analysis or visualization.
💼 Career
Data analysts and scientists often reshape data to fit the needs of different tools or to better understand patterns.
Progress0 / 4 steps
1
Create the initial sales DataFrame
Import pandas as pd and create a DataFrame called sales with these exact values: columns 'Day', 'Sourdough', 'Baguette', 'Rye' and rows for 'Monday', 'Tuesday', 'Wednesday' with sales numbers 20, 35, 30 for Sourdough; 15, 40, 25 for Baguette; and 10, 20, 15 for Rye.
Data Analysis Python
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 variable called bread_types that holds the list ['Sourdough', 'Baguette', 'Rye'].
Data Analysis Python
Need a hint?

Just assign the list of bread names to the variable bread_types.

3
Reshape the DataFrame from wide to long format
Use pd.melt on the sales DataFrame to reshape it. Use Day as the id variable, bread_types as the value variables, and name the variable column 'Bread' and the value column 'Sales'. Store the result in a variable called long_sales.
Data Analysis Python
Need a hint?

Use pd.melt with id_vars for the column to keep, value_vars for columns to unpivot, and set var_name and value_name for new column names.

4
Transpose and print the reshaped DataFrame
Create a variable called transposed_sales by transposing long_sales using the .T attribute. Then print transposed_sales.
Data Analysis Python
Need a hint?

Use .T to transpose the DataFrame and then print it.