0
0
Pandasdata~15 mins

dtypes for column data types in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding dtypes for column data types in pandas
📖 Scenario: You work in a small shop and keep track of your sales data in a table. You want to understand what kind of data each column holds so you can analyze it better.
🎯 Goal: You will create a pandas DataFrame with sales data, then check the data types of each column using dtypes.
📋 What You'll Learn
Create a pandas DataFrame with specific sales data
Use the dtypes attribute to find the data types of each column
Print the data types to see the result
💡 Why This Matters
🌍 Real World
Understanding data types helps you clean and analyze data correctly, like knowing which columns hold numbers, text, or dates.
💼 Career
Data scientists and analysts often check data types to prepare data for analysis, modeling, and visualization.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales with these exact columns and values:
'Product': ['Apple', 'Banana', 'Carrot'],
'Quantity': [10, 5, 7],
'Price': [0.5, 0.3, 0.2],
'Date': ['2024-06-01', '2024-06-02', '2024-06-03']
Pandas
Need a hint?

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

2
Add a configuration to convert the Date column to datetime
Create a variable called date_column and set it to the string 'Date' to identify the date column.
Pandas
Need a hint?

This variable will help us later to convert the date column.

3
Convert the Date column to datetime and check dtypes
Use pd.to_datetime() to convert the sales[date_column] column to datetime format. Then assign it back to sales[date_column]. Finally, create a variable called column_types and set it to sales.dtypes to get the data types of all columns.
Pandas
Need a hint?

Use pd.to_datetime() to convert strings to dates. Then use sales.dtypes to get the data types.

4
Print the data types of each column
Print the variable column_types to display the data types of all columns in the sales DataFrame.
Pandas
Need a hint?

Just use print(column_types) to see the data types.