Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The 'dtype' attribute shows the data type of a single column in a DataFrame.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The 'dtypes' attribute returns a Series with data types of all columns.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' on a Series causes an error.
Using 'type' returns Python type, not pandas dtype.
✗ Incorrect
Accessing 'dtype' on a Series (a single column) returns its data type correctly.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' for a single column which causes errors.
Comparing to wrong data type string.
✗ Incorrect
Use 'dtype' to get the data type of each column and compare it to 'int64' to filter integer columns.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'dtypes' for single column data type.
Using the column name directly without uppercasing.
✗ Incorrect
Use 'col.upper()' for uppercase keys, 'col' for iterating columns, and 'dtype' to get each column's data type.