0
0
Pandasdata~30 mins

Using appropriate dtypes in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Using appropriate dtypes
📖 Scenario: You work in a small store that tracks sales data. You have a table with product names, quantities sold, and prices. You want to make sure the data uses the best types to save memory and make calculations faster.
🎯 Goal: You will create a pandas DataFrame with sales data, set a configuration for the data type of the quantity column, convert the quantity column to that type, and then print the DataFrame info to see the changes.
📋 What You'll Learn
Create a pandas DataFrame with product names, quantities sold, and prices.
Create a variable to hold the desired data type for the quantity column.
Convert the quantity column to the desired data type using the variable.
Print the DataFrame info to show the data types and memory usage.
💡 Why This Matters
🌍 Real World
Using the right data types helps save memory and speeds up data processing in real-world data science projects.
💼 Career
Data scientists and analysts often optimize data storage by choosing appropriate dtypes to handle large datasets efficiently.
Progress0 / 4 steps
1
Create the sales DataFrame
Create a pandas DataFrame called sales with these exact columns and values:
'Product': ['Apple', 'Banana', 'Cherry', 'Date']
'Quantity': [10, 20, 15, 5]
'Price': [0.5, 0.3, 0.2, 1.0]
Pandas
Need a hint?

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

2
Set the desired dtype for Quantity
Create a variable called quantity_dtype and set it to the string 'int8' to specify the data type for the Quantity column.
Pandas
Need a hint?

Just assign the string 'int8' to the variable quantity_dtype.

3
Convert Quantity column to the desired dtype
Use the astype() method on the Quantity column of sales to convert it to the data type stored in quantity_dtype. Assign the result back to the Quantity column.
Pandas
Need a hint?

Use sales['Quantity'].astype(quantity_dtype) and assign it back to sales['Quantity'].

4
Print DataFrame info to see dtypes and memory usage
Use print() to display the output of sales.info() so you can see the data types and memory usage of the DataFrame.
Pandas
Need a hint?

Call sales.info() inside print() to show the DataFrame details.