0
0
Pandasdata~10 mins

Selecting columns by name 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 select the column 'Age' from the DataFrame df.

Pandas
age_column = df[[1]]
Drag options to blanks, or click blank then click option'
A'age'
Bdf['Age']
CAge
D'Age'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the column name.
Using the variable name without quotes.
Trying to select the column without brackets.
2fill in blank
medium

Complete the code to select multiple columns 'Name' and 'Salary' from the DataFrame df.

Pandas
selected_columns = df[[1]]
Drag options to blanks, or click blank then click option'
A('Name', 'Salary')
B['Name', 'Salary']
C'Name', 'Salary'
D{'Name', 'Salary'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple or set instead of a list.
Not using brackets around the list.
Passing column names as a single string.
3fill in blank
hard

Fix the error in the code to select the 'City' column from df.

Pandas
city = df.[1]
Drag options to blanks, or click blank then click option'
ACity
B'City'
Ccity
D['City']
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes inside dot notation.
Using brackets with dot notation.
Using lowercase when column name is capitalized.
4fill in blank
hard

Fill both blanks to select columns 'Height' and 'Weight' from df and assign to new_df.

Pandas
new_df = df[[1]]
print(new_df.columns[[2]])
Drag options to blanks, or click blank then click option'
A['Height', 'Weight']
B['Height', 'Weight', 'Age']
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using a tuple or set instead of a list for columns.
Using wrong index to access columns.
Including extra columns in the list.
5fill in blank
hard

Fill all three blanks to create a dictionary with column names as keys and their data types as values.

Pandas
col_types = [1]: df[[2]].dtype for [3] in df.columns}
Drag options to blanks, or click blank then click option'
Acol
Dcolumn
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names in different blanks.
Not using the variable to select the column.
Using df.columns directly as key.