We check data types to understand what kind of information each column holds. This helps us decide how to work with the data correctly.
Checking data types in Data Analysis Python
dataframe.dtypes
This command shows the data type of each column in a DataFrame.
Data types include int64 (integers), float64 (decimal numbers), object (text), bool (True/False), and datetime64[ns] (dates).
import pandas as pd data = {'age': [25, 30, 22], 'name': ['Alice', 'Bob', 'Charlie']} df = pd.DataFrame(data) print(df.dtypes)
print(type(df['age'][0]))
print(df['name'].dtype)
This program creates a DataFrame with different types of data: numbers, text, booleans, and dates. Then it prints the data type of each column to understand the data.
import pandas as pd # Create a sample dataset data = { 'temperature': [22.5, 23.0, 21.8, 22.1], 'city': ['Paris', 'London', 'Berlin', 'Madrid'], 'rainy': [False, True, False, True], 'date': pd.to_datetime(['2024-06-01', '2024-06-02', '2024-06-03', '2024-06-04']) } # Create DataFrame weather_df = pd.DataFrame(data) # Check data types of each column print(weather_df.dtypes)
Sometimes numbers are stored as text (object type). You may need to convert them to numeric types for calculations.
Use pd.to_datetime() to convert text to date types.
Knowing data types helps avoid errors when analyzing or visualizing data.
Checking data types helps you understand your data better.
Use dataframe.dtypes to see each column's type.
Correct data types are important for proper analysis and calculations.
Practice
DataFrame.dtypes attribute show in a pandas DataFrame?Solution
Step 1: Understand what
Thedtypesmeansdtypesattribute in pandas shows the data type of each column in a DataFrame.Step 2: Compare options with the definition
Only The data type of each column in the DataFrame ('The data type of each column in the DataFrame') correctly describes this: it shows the data type of each column.Final Answer:
The data type of each column in the DataFrame -> Option AQuick Check:
DataFrame.dtypes = column types [OK]
- Confusing dtypes with DataFrame shape
- Thinking dtypes shows data values
- Mixing dtypes with summary statistics
df?Solution
Step 1: Recall pandas syntax for data types
In pandas,dtypesis an attribute (not a method), so it is accessed without parentheses.Step 2: Evaluate each option
df.dtypes usesdf.dtypescorrectly. Options A, B, and C use incorrect method names or parentheses.Final Answer:
df.dtypes -> Option BQuick Check:
Use attribute dtypes without parentheses [OK]
- Adding parentheses after dtypes
- Using incorrect method names
- Confusing dtypes with type() function
import pandas as pd
data = {'age': [25, 30], 'name': ['Alice', 'Bob'], 'score': [88.5, 92.0]}
df = pd.DataFrame(data)
print(df.dtypes)What will be the output?
Solution
Step 1: Check data types of each column in the dictionary
Column 'age' has integers (25, 30) so type is int64 by default. 'name' has strings, so type is object. 'score' has floats (88.5, 92.0), so type is float64.Step 2: Confirm pandas default data types
Pandas uses int64 for integers, object for strings, and float64 for floats by default.Final Answer:
age int64 name object score float64 dtype: object -> Option AQuick Check:
Integers=int64, Strings=object, Floats=float64 [OK]
- Assuming strings have type 'string' instead of 'object'
- Mixing int and float types
- Confusing int32 with int64 default
import pandas as pd
data = {'A': [1, 2], 'B': ['x', 'y']}
df = pd.DataFrame(data)
print(df.dtypes())But it gives an error. What is the problem?
Solution
Step 1: Identify the error cause
The code usesdf.dtypes()with parentheses, butdtypesis an attribute, not a method.Step 2: Correct the syntax
Remove parentheses and usedf.dtypesto get the data types without error.Final Answer:
You should use df.dtypes without parentheses -> Option DQuick Check:
dtypes is attribute, not method [OK]
- Adding parentheses after dtypes
- Thinking DataFrame is empty causes error
- Believing numpy import is needed for dtypes
df with columns: ['id', 'value', 'flag']. The flag column contains only 0 and 1 but is stored as integers. You want to convert flag to a boolean type for better memory use. Which code correctly changes the data type?Solution
Step 1: Understand how to convert column types in pandas
Use theastype()method to convert a column to a different data type.Step 2: Check each option's validity
df['flag'] = df['flag'].astype(bool) usesastype(bool)correctly. df['flag'] = bool(df['flag']) tries to convert the whole Series to bool, which returns a single True/False, not a column. Options C and D use non-existent methods.Final Answer:
df['flag'] = df['flag'].astype(bool) -> Option CQuick Check:
Use astype(bool) to convert column to boolean [OK]
- Using bool() on entire Series instead of astype
- Trying non-existent methods like to_bool()
- Confusing convert() with astype()
Ready to prove it?
Pass the test and this pattern moves unknown
