📖 Scenario: Imagine you are analyzing sales data for a small store. The data includes the number of items sold and the total sales amount. You want to make sure the numbers are stored correctly to do calculations later.
🎯 Goal: You will create a pandas DataFrame with sales data, check and set the numeric types for the columns to int64 and float64, and then display the data types to confirm.
📋 What You'll Learn
Create a pandas DataFrame with specific sales data
Check and set the data types of columns to int64 and float64
Print the data types of the DataFrame columns
💡 Why This Matters
🌍 Real World
Data scientists often need to ensure numeric data is stored in the correct format for calculations, memory efficiency, and compatibility with analysis tools.
💼 Career
Understanding and managing numeric types is essential for data cleaning, preprocessing, and preparing data for machine learning or reporting.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_data with two columns: 'items_sold' and 'total_sales'. Use these exact values: items_sold as [10, 15, 7, 20], and total_sales as [250.0, 375.5, 175.75, 500.0].
Pandas
Hint
Use pd.DataFrame with a dictionary containing the two columns and their values.
2
Check the current data types
Create a variable called current_types that stores the data types of the columns in sales_data using the dtypes attribute.
Pandas
Hint
Use sales_data.dtypes to get the data types of each column.
3
Set the correct numeric types for columns
Use the astype method to convert the 'items_sold' column to int64 and the 'total_sales' column to float64. Update the sales_data DataFrame with these new types.
Pandas
Hint
Use sales_data.astype({'items_sold': 'int64', 'total_sales': 'float64'}) to set the types.
4
Print the data types to confirm
Print the data types of the sales_data DataFrame columns using the dtypes attribute.
Pandas
Hint
Use print(sales_data.dtypes) to show the column types.
Practice
(1/5)
1. Which pandas numeric type is best for storing whole numbers without decimals?
easy
A. bool
B. float64
C. int64
D. object
Solution
Step 1: Understand numeric types in pandas
int64 is used for integers (whole numbers), float64 for decimals.
Step 2: Identify the type for whole numbers
Whole numbers require int64 because float64 stores decimals.
Final Answer:
int64 -> Option C
Quick Check:
Whole numbers = int64 [OK]
Hint: Whole numbers use int64, decimals use float64 [OK]
Common Mistakes:
Confusing float64 as default for all numbers
Choosing object type for numeric data
Using bool for numeric storage
2. Which of the following is the correct way to convert a pandas DataFrame column df['col'] to float64 type?
easy
A. df.astype('float64')['col']
B. df['col'].convert('float64')
C. astype(df['col'], 'float64')
D. df['col'].astype('float64')
Solution
Step 1: Recall pandas method to change column type
The method to change a column's type is astype() called on the column.
Step 2: Apply correct syntax
Use df['col'].astype('float64') to convert the column to float64.
Final Answer:
df['col'].astype('float64') -> Option D
Quick Check:
astype() on column = correct syntax [OK]
Hint: Use astype() on the column to change type [OK]
Common Mistakes:
Calling astype() on the whole DataFrame incorrectly
Using non-existent convert() method
Wrong function syntax like astype(df['col'], 'float64')
int64 must be passed as a string 'int64', not as a bare name.
Step 2: Understand Python name error
Without quotes, Python looks for a variable named int64 which is undefined.
Final Answer:
int64 is not defined without quotes -> Option A
Quick Check:
Use quotes around type names in astype() [OK]
Hint: Always quote type names in astype() [OK]
Common Mistakes:
Passing type names without quotes
Thinking astype() needs import
Believing columns cannot be reassigned
5. You have a DataFrame df with a column 'values' containing numbers as strings, e.g., ['1', '2.5', '3']. You want to convert this column to numeric types efficiently, storing integers as int64 and decimals as float64 where needed. Which approach is best?
hard
A. Use pd.to_numeric(df['values'], downcast='integer')
B. Use pd.to_numeric(df['values'], errors='coerce') and then convert integers to int64
C. Use df['values'] = df['values'].astype('float64') for all values
D. Manually check each value and convert to int64 or float64
Solution
Step 1: Understand mixed numeric strings
Strings include integers and decimals, so direct astype('float64') converts all to float.
Step 2: Use pd.to_numeric with errors='coerce'
This converts strings to numbers, coercing errors to NaN, allowing safe conversion.
Step 3: Convert integers to int64 after identifying them
After conversion, integers can be cast to int64 to save memory.
Final Answer:
Use pd.to_numeric(df['values'], errors='coerce') and then convert integers to int64 -> Option B
Quick Check:
pd.to_numeric + careful conversion handles mixed types best [OK]
Hint: Use pd.to_numeric with errors='coerce' for mixed numeric strings [OK]
Common Mistakes:
Converting all to float64 losing integer type
Trying manual conversion for each value
Ignoring errors in conversion causing crashes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.