📖 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
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
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
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
Hint
Call sales.info() inside print() to show the DataFrame details.
Practice
(1/5)
1. Why is it important to use appropriate data types in a pandas DataFrame?
easy
A. It automatically fixes missing data values.
B. It reduces memory usage and speeds up data processing.
C. It makes the DataFrame look colorful in output.
D. It changes the order of rows in the DataFrame.
Solution
Step 1: Understand data types impact on memory
Using smaller or correct data types reduces the memory needed to store data.
Step 2: Understand data types impact on speed
Operations on smaller data types run faster, improving performance.
Final Answer:
It reduces memory usage and speeds up data processing. -> Option B
Quick Check:
Appropriate dtypes = less memory and faster [OK]
Hint: Smaller data types save memory and speed up work [OK]
Common Mistakes:
Thinking data types change data values automatically
Believing data types affect DataFrame colors
Assuming data types reorder rows
2. Which of the following is the correct way to change a column named age to integer type in pandas?
easy
A. df['age'] = df['age'].astype('int')
B. df.astype('age', int)
C. df['age'].convert(int)
D. df.change_type('age', 'int')
Solution
Step 1: Recall pandas syntax for changing dtype
The correct method is to use astype() on the column.
Step 2: Check method usage
Use df['age'] = df['age'].astype('int') to convert the 'age' column to integer.
Final Answer:
df['age'] = df['age'].astype('int') -> Option A
Quick Check:
astype() method changes dtype [OK]
Hint: df[col] = df[col].astype(dtype) to change dtype [OK]
Common Mistakes:
Forgetting to assign the result of astype() back to the column
Using non-existent methods like convert() or change_type()
Passing arguments in wrong order or syntax
3. Given the code below, what will be the output of df.dtypes?
Why might this cause an error or unexpected behavior?
medium
A. Converting numeric to category without checking unique values may increase memory usage or cause unexpected behavior.
B. You cannot convert a column twice in pandas.
C. astype('category') only works on string columns.
D. int64 is not a valid dtype in pandas.
Solution
Step 1: Understand dtype conversion to category
Converting numeric to category is allowed but categories are based on unique values.
Step 2: Consider memory and behavior risks
If the numeric column has many unique values, category conversion may increase memory or cause unexpected grouping.
Final Answer:
Converting numeric to category without checking unique values may increase memory usage or cause unexpected behavior. -> Option A
Quick Check:
Category dtype needs careful unique value check [OK]
Hint: Check unique values before converting to category [OK]
Common Mistakes:
Thinking multiple astype() calls are invalid
Believing astype('category') only works on strings
Assuming int64 is invalid dtype
5. You have a large DataFrame with a column status containing only three unique string values: 'new', 'in_progress', 'done'. Which dtype change will save memory and keep data efficient?
hard
A. Keep status as string without changes.
B. Convert status to object dtype.
C. Convert status to int64 dtype directly.
D. Convert status to category dtype.
Solution
Step 1: Identify unique values in 'status'
Only three unique strings exist, ideal for categorical dtype.
Step 2: Understand category dtype benefits
Category stores unique values once and uses integer codes internally, saving memory.
Step 3: Compare other options
Object dtype stores full strings repeatedly; int64 conversion is invalid without mapping; no change wastes memory.
Final Answer:
Convert status to category dtype. -> Option D
Quick Check:
Few unique strings? Use category dtype [OK]
Hint: Few unique strings? Use category dtype for memory [OK]
Common Mistakes:
Trying to convert strings directly to int64 without mapping
Keeping object dtype wastes memory
Ignoring category dtype benefits
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.