0
0
Pandasdata~10 mins

dtypes and data type checking in Pandas - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - dtypes and data type checking
Create DataFrame
Check dtypes attribute
Get data types of each column
Use dtype-checking functions
Decide actions based on types
Start with a DataFrame, check its dtypes attribute to see each column's data type, then use functions to check or convert types.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [1.1, 2.2, 3.3],
    'C': ['x', 'y', 'z']
})
print(df.dtypes)
Create a DataFrame with integer, float, and string columns, then print their data types.
Execution Table
StepActionCode/ExpressionResult/Output
1Create DataFramedf = pd.DataFrame({'A':[1,2,3],'B':[1.1,2.2,3.3],'C':['x','y','z']})DataFrame with 3 columns A(int), B(float), C(object)
2Check dtypes attributedf.dtypesA int64 B float64 C object dtype: object
3Check if column A is integer typepd.api.types.is_integer_dtype(df['A'])True
4Check if column B is float typepd.api.types.is_float_dtype(df['B'])True
5Check if column C is string/object typepd.api.types.is_object_dtype(df['C'])True
6Convert column C to category typedf['C'] = df['C'].astype('category')Column C dtype changed to category
7Check new dtypesdf.dtypesA int64 B float64 C category dtype: object
8ExitNo more stepsEnd of execution
💡 All columns checked and one converted; execution ends.
Variable Tracker
VariableStartAfter Step 1After Step 6Final
dfundefinedDataFrame with A:int64, B:float64, C:objectDataFrame with A:int64, B:float64, C:categorySame as after Step 6
Key Moments - 3 Insights
Why does column C show dtype 'object' initially?
Because pandas uses 'object' dtype for string columns by default, as shown in execution_table step 2.
How do we check if a column is of a specific type?
Use pandas functions like is_integer_dtype or is_float_dtype as in steps 3 and 4 of the execution_table.
What happens when we convert a column to 'category' dtype?
The column's dtype changes to 'category', which can save memory and speed up operations, as seen in step 6 and confirmed in step 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the dtype of column B?
Afloat64
Bint64
Cobject
Dcategory
💡 Hint
Check the 'Result/Output' column in step 2 of execution_table.
At which step does column C change its dtype to 'category'?
AStep 5
BStep 6
CStep 3
DStep 7
💡 Hint
Look for the action mentioning 'astype' conversion in execution_table.
If we check is_integer_dtype on column B at step 4, what is the result?
ATrue
BError
CFalse
DNone
💡 Hint
Refer to the 'Result/Output' in step 4 of execution_table.
Concept Snapshot
pandas DataFrame columns have dtypes showing data type.
Use df.dtypes to see all column types.
Check types with pd.api.types functions.
Convert types with astype() method.
Category dtype saves memory for repeated strings.
Full Transcript
We start by creating a pandas DataFrame with three columns: A with integers, B with floats, and C with strings. We then check the data types of each column using the df.dtypes attribute, which shows A as int64, B as float64, and C as object (pandas uses object for strings). Next, we use pandas type-checking functions like is_integer_dtype and is_float_dtype to confirm the types of columns A and B. We also check if column C is of object type. Then, we convert column C to the 'category' data type using astype('category'), which can improve performance and reduce memory. Finally, we verify the new data types with df.dtypes again, seeing that column C is now category. This process helps us understand and manage data types in pandas DataFrames effectively.