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.