0
0
Pandasdata~10 mins

dtypes for column data types 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'
Atype
Bdtype
Cinfo
Ddtypes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' instead of 'dtype' for a single column.
Using 'type' which returns the Python type of the object, not the data type.
2fill in blank
medium

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

Pandas
import pandas as pd
data = {'height': [5.5, 6.0], 'weight': [130, 180]}
df = pd.DataFrame(data)
print(df.[1])
Drag options to blanks, or click blank then click option'
Adtypes
Bdtype
Ctype
Dinfo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtype' which works only for a single column.
Using 'info' which prints summary but not a Series of dtypes.
3fill in blank
hard

Fix the error in the code to print the data type of the 'score' column.

Pandas
import pandas as pd
data = {'score': [88, 92, 85]}
df = pd.DataFrame(data)
print(df.score.[1])
Drag options to blanks, or click blank then click option'
Ainfo
Bdtypes
Ctype
Ddtype
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' on a Series causes an error.
Using 'type' returns Python type, not pandas dtype.
4fill in blank
hard

Fill both blanks to create a dictionary of column names and their data types for columns with integer type.

Pandas
import pandas as pd
data = {'a': [1, 2], 'b': [3.5, 4.5], 'c': [5, 6]}
df = pd.DataFrame(data)
int_cols = {col: df[col].[1] for col in df.columns if df[col].[2] == 'int64'}
print(int_cols)
Drag options to blanks, or click blank then click option'
Adtype
Bdtypes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' for a single column which causes errors.
Comparing to wrong data type string.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase column names as keys and their data types as values for columns with float type.

Pandas
import pandas as pd
data = {'x': [1.1, 2.2], 'y': [3, 4], 'z': [5.5, 6.6]}
df = pd.DataFrame(data)
float_cols = [1]: df[[2]].[3] for [2] in df.columns if df[[2]].dtype == 'float64'}
print(float_cols)
Drag options to blanks, or click blank then click option'
Acol.upper()
Bcol
Cdtype
Ddtypes
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' for single column data type.
Using the column name directly without uppercasing.