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
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
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
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
Hint
Just use print(column_types) to see the data types.
Practice
(1/5)
1. What does the dtypes attribute in a pandas DataFrame tell you?
easy
A. The names of the columns
B. The number of rows in the DataFrame
C. The total memory used by the DataFrame
D. The type of data stored in each column
Solution
Step 1: Understand what dtypes means
The dtypes attribute shows the data type of each column in a DataFrame, like integer, float, or string.
Step 2: Compare options with the definition
Only the type of data stored in each column correctly describes that dtypes tells the type of data stored in each column.
Final Answer:
The type of data stored in each column -> Option D
Quick Check:
dtypes = data type per column [OK]
Hint: Remember: dtypes show column data types, not counts or names [OK]
Common Mistakes:
Confusing dtypes with DataFrame shape
Thinking dtypes shows column names
Assuming dtypes shows memory usage
2. Which of the following is the correct way to check the data types of columns in a pandas DataFrame named df?
easy
A. df.dtypes
B. df.types()
C. df.get_dtypes()
D. df.columns.types
Solution
Step 1: Recall the correct pandas syntax
To check column data types, pandas uses the attribute dtypes without parentheses.
Step 2: Evaluate each option
df.dtypes uses df.dtypes, which is correct. Options B and C use incorrect method names, and D is not a valid attribute.
Final Answer:
df.dtypes -> Option A
Quick Check:
Use df.dtypes to check column types [OK]
Hint: Use df.dtypes without parentheses to see column types [OK]
Step 1: Identify data types from the data dictionary
Column 'A' has integers (1,2,3) so type is int64 by default. Column 'B' has floats (1.5,2.5,3.5) so type is float64. Column 'C' has strings ('x','y','z') which pandas treats as object type.
Step 2: Match with the printed output format
The output shows each column name with its dtype, followed by 'dtype: object'. A int64
B float64
C object
dtype: object matches this exactly.
Final Answer:
A int64
B float64
C object
dtype: object -> Option C
The code uses df.dtypes() with parentheses, but dtypes is an attribute, not a method.
Step 2: Correct the syntax
Remove the parentheses and use df.dtypes to get the data types without error.
Final Answer:
You should use df.dtypes without parentheses -> Option B
Quick Check:
dtypes is attribute, not method [OK]
Hint: Remember: dtypes has no parentheses, it's not a function [OK]
Common Mistakes:
Calling dtypes as a function
Thinking DataFrame is empty causes error
Believing numpy import is needed for dtypes
5. You have a DataFrame df with columns: age (integers), income (floats), and member (True/False). You want to check the data types and convert member to integer (1 for True, 0 for False). Which code correctly does this?
hard
A. print(df.dtypes)
df['member'] = df['member'].astype(int)
B. print(df.types)
df['member'] = int(df['member'])
C. print(df.dtypes())
df['member'] = df['member'].to_int()
D. print(df.columns)
df['member'] = df['member'].astype('bool')
Solution
Step 1: Check data types correctly
Use df.dtypes (no parentheses) to print data types. Options B and C use incorrect syntax.
Step 2: Convert boolean column to integer
Use astype(int) to convert True/False to 1/0. print(df.dtypes)
df['member'] = df['member'].astype(int) does this correctly. print(df.columns)
df['member'] = df['member'].astype('bool') converts to bool again, which is unnecessary.
Final Answer:
print(df.dtypes)
df['member'] = df['member'].astype(int) -> Option A
Quick Check:
Use df.dtypes and astype(int) for boolean to int [OK]
Hint: Use df.dtypes and astype(int) to convert booleans to 0/1 [OK]
Common Mistakes:
Calling dtypes as a method
Trying to convert column with int() directly
Converting boolean to bool again instead of int
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.