This quiz helps to test knowledge and skills in efficiently selecting, filtering, and manipulating data using Python’s powerful Pandas library.
Question 1
What is the correct way to select a column named sales from a DataFrame df?
df.sales
df['sales']
df[0]
df.select('sales')
Question 2
How do you select rows where the sales column is greater than 500?
import pandas as pd
df = pd.DataFrame({'sales': [300, 700, 200, 900]})
df[df.sales > 500]
df[df['sales'] > 500]
df[df['sales'] < 500]
Both 1 and 2
Question 3
Which method is used to select specific rows and columns by labels?
.iloc
.loc
.query
.filter
Question 4
How do you select rows 2 to 4 (inclusive) and columns A and B using .loc?
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3, 4, 5], 'B': [6, 7, 8, 9, 10], 'C': [11, 12, 13, 14, 15]})
df.loc[2:4, ['A', 'B']]
df.iloc[2:4, ['A', 'B']]
df.loc[1:3, ['A', 'B']]
df.loc[:, ['A', 'B']]
Question 5
How do you select rows where column C contains the value 15?
df[df['C'] == 15]
df[df.C == 15]
df[df['C'] > 15]
Both 1 and 2
Question 6
What is the output of the following code?
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
print(df.iloc[1:3, 0])
1, 2
2, 3
1, 3
2
Question 7
How can you filter rows where column A is greater than 3 and column B is less than 10?
import panads as pd
df = pd.DataFrame({'A': [1, 2, 4, 5], 'B': [6, 7, 8, 9]})
df[(df['A'] > 3) & (df['B'] < 10)]
df[df['A'] > 3 & df['B'] < 10]
df[df['A'] > 3 | df['B'] < 10]
df.query('A > 3 and B < 10')
Question 8
How do you drop rows with missing values in column A?
df.dropna()
df.dropna(subset=['A'])
df.fillna(0)
df.drop(columns=['A'])
Question 9
What does the following code return?
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.loc[0:1, 'A':'B']
Rows 0 to 1 and columns A to B
Rows 0 to 1 and only column A
An error
All rows and columns
Question 10
What is the most efficient way to filter rows where the category column contains "electronics"?
df[df['category'] == 'electronics']
df[df['category'].str.contains('electronics')]
df.query('category == "electronics"')
df.filter('electronics')
There are 10 questions to complete.