0
0
Pandasdata~10 mins

dtypes and data type checking in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check the data type of the 'age' column in the DataFrame.

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob'], 'age': [25, 30]}
df = pd.DataFrame(data)
print(df['age'].[1])
Drag options to blanks, or click blank then click option'
Adtype
Bdtypes
Ctype
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' which is for the whole DataFrame.
Using 'type' which returns the Python type of the object, not the data type of the column.
2fill in blank
medium

Complete the code to get the data types of all columns in the DataFrame.

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob'], 'age': [25, 30]}
df = pd.DataFrame(data)
print(df.[1])
Drag options to blanks, or click blank then click option'
Ainfo
Bdtype
Ctype
Ddtypes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtype' which works only for a single column.
Using 'info' which prints summary but does not return data types directly.
3fill in blank
hard

Fix the error in the code to check if the 'score' column is of integer type.

Pandas
import pandas as pd

data = {'score': [88, 92, 79]}
df = pd.DataFrame(data)
print(df['score'].dtype == '[1]')
Drag options to blanks, or click blank then click option'
Aint64
Binteger
Cint32
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'int' which is a Python type, not a pandas dtype.
Using 'integer' which is not a valid dtype string.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps column names to their data types for columns with object type.

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob'], 'age': [25, 30], 'city': ['NY', 'LA']}
df = pd.DataFrame(data)
obj_cols = {col: df[col].[1] for col in df.columns if df[col].[2] == 'object'}
print(obj_cols)
Drag options to blanks, or click blank then click option'
Adtype
Bdtypes
Cdtype.name
Ddtype.type
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' which is for DataFrame, not Series.
Using 'dtype' alone which returns a dtype object, not a string.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps column names to their data types for columns with numeric types.

Pandas
import pandas as pd
import numpy as np

data = {'height': [5.5, 6.0], 'weight': [130, 150], 'name': ['Alice', 'Bob']}
df = pd.DataFrame(data)
numeric_cols = {col: df[col].[1] for col in df.columns if np.[2](df[col].dtype, np.number) and df[col].dtype != '[3]'}
print(numeric_cols)
Drag options to blanks, or click blank then click option'
Adtype
Bdtypes
Cissubdtype
Disnumeric
Eint64
Fobject
Gfloat64
Hint32
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' instead of 'dtype' for Series.
Using 'isnumeric' which is not a numpy function.
Comparing dtype to 'int64' or 'float64' directly instead of excluding 'object'.