Bird
Raised Fist0
Pandasdata~10 mins

dtypes for column data types in Pandas - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - dtypes for column data types
Create DataFrame
↓
Access .dtypes
↓
Return Series
↓
Show each column's data type
↓
Use for analysis or debugging
We create a DataFrame, then access its .dtypes attribute to get a Series showing each column's data type.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({
  'A': [1, 2],
  'B': [3.5, 4.5],
  'C': ['x', 'y']
})
print(df.dtypes)
This code creates a DataFrame with three columns and prints the data types of each column.
Execution Table
StepActionCode/ExpressionResult
1Create DataFramedf = pd.DataFrame({'A': [1, 2], 'B': [3.5, 4.5], 'C': ['x', 'y']})DataFrame with columns A, B, C
2Access dtypes attributedf.dtypesSeries showing: A int64, B float64, C object
3Print dtypesprint(df.dtypes)Output: A int64 B float64 C object dtype: object
💡 All columns' data types are shown as a Series; no further steps.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
dfundefinedDataFrame with columns A, B, CSame DataFrameSame DataFrame
df.dtypesundefinedundefinedSeries with dtypesSeries with dtypes
Key Moments - 2 Insights
Why does the 'C' column show 'object' instead of 'string'?
In pandas, text columns are stored as 'object' dtype by default, which means they hold Python objects like strings. See execution_table step 2 where 'C' is 'object'.
Is df.dtypes a DataFrame or something else?
df.dtypes returns a pandas Series, not a DataFrame. It lists each column name as index and its data type as values, as shown in execution_table step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the dtype of column 'B' after step 2?
Aobject
Bint64
Cfloat64
Dbool
💡 Hint
Check the 'Result' column in execution_table row for step 2.
At which step is the DataFrame 'df' created?
AStep 2
BStep 1
CStep 3
DNo step creates it
💡 Hint
Look at the 'Action' column in execution_table for when df is assigned.
If column 'A' had float numbers instead of integers, how would df.dtypes change?
AColumn 'A' dtype would be float64
BColumn 'A' dtype would be int64
CColumn 'A' dtype would be object
DNo change in dtypes
💡 Hint
Refer to variable_tracker and how dtypes reflect actual data types.
Concept Snapshot
Use df.dtypes to see each column's data type in a DataFrame.
It returns a Series with column names as index and dtypes as values.
Common dtypes: int64 for integers, float64 for decimals, object for text.
Useful for checking data before analysis or cleaning.
Full Transcript
We start by creating a pandas DataFrame with three columns: A with integers, B with floats, and C with strings. Then, we access the .dtypes attribute of the DataFrame, which returns a Series showing the data type of each column. The output shows that column A is int64, B is float64, and C is object (which pandas uses for text). This helps us understand what kind of data each column holds. The variable tracker shows how df and df.dtypes change during execution. Key moments clarify why text columns show as object and that df.dtypes returns a Series, not a DataFrame. The quiz tests understanding of dtypes and execution steps. Finally, the snapshot summarizes how to use df.dtypes to check column 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

  1. Step 1: Understand what dtypes means

    The dtypes attribute shows the data type of each column in a DataFrame, like integer, float, or string.
  2. 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.
  3. Final Answer:

    The type of data stored in each column -> Option D
  4. 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

  1. Step 1: Recall the correct pandas syntax

    To check column data types, pandas uses the attribute dtypes without parentheses.
  2. 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.
  3. Final Answer:

    df.dtypes -> Option A
  4. Quick Check:

    Use df.dtypes to check column types [OK]
Hint: Use df.dtypes without parentheses to see column types [OK]
Common Mistakes:
  • Adding parentheses after dtypes
  • Using non-existent methods like get_dtypes()
  • Confusing dtypes with columns attribute
3. Given the following code:
import pandas as pd
data = {'A': [1, 2, 3], 'B': [1.5, 2.5, 3.5], 'C': ['x', 'y', 'z']}
df = pd.DataFrame(data)
print(df.dtypes)

What will be the output?
medium
A. A object B object C object dtype: object
B. A float64 B int64 C object dtype: object
C. A int64 B float64 C object dtype: object
D. A int32 B float32 C string dtype: object

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    A int64 B float64 C object dtype: object -> Option C
  4. Quick Check:

    Integers=int64, floats=float64, strings=object [OK]
Hint: Integers default to int64, floats to float64, strings to object [OK]
Common Mistakes:
  • Assuming strings show as 'string' dtype
  • Confusing int32 with int64
  • Mixing float and int types
4. You run this code:
import pandas as pd
df = pd.DataFrame({'X': [1, 2, 3], 'Y': ['a', 'b', 'c']})
print(df.dtypes())

But get an error. What is the problem?
medium
A. The DataFrame is empty
B. You should use df.dtypes without parentheses
C. Column names must be integers
D. You need to import numpy first

Solution

  1. Step 1: Identify the error cause

    The code uses df.dtypes() with parentheses, but dtypes is an attribute, not a method.
  2. Step 2: Correct the syntax

    Remove the parentheses and use df.dtypes to get the data types without error.
  3. Final Answer:

    You should use df.dtypes without parentheses -> Option B
  4. 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

  1. Step 1: Check data types correctly

    Use df.dtypes (no parentheses) to print data types. Options B and C use incorrect syntax.
  2. 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.
  3. Final Answer:

    print(df.dtypes) df['member'] = df['member'].astype(int) -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard