0
0
Pandasdata~15 mins

Numeric types (int64, float64) in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with Numeric Types in pandas
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use print(sales_data.dtypes) to show the column types.