0
0
Pandasdata~20 mins

Dropping columns and rows in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Drop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output after dropping rows by index
What is the output DataFrame after dropping rows with index 1 and 3?
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [10, 20, 30, 40, 50],
    'B': [5, 4, 3, 2, 1]
})
result = df.drop([1, 3])
print(result)
A
   A  B
1  20  4
3  40  2
B
   A  B
0  10  5
2  30  3
4  50  1
C
   A  B
0  10  5
1  20  4
2  30  3
3  40  2
4  50  1
D
   A  B
0  10  5
2  30  3
3  40  2
4  50  1
Attempts:
2 left
💡 Hint
Use df.drop() with a list of row indices to remove those rows.
data_output
intermediate
2:00remaining
Resulting DataFrame after dropping columns
What is the resulting DataFrame after dropping columns 'X' and 'Z'?
Pandas
import pandas as pd

df = pd.DataFrame({
    'X': [1, 2, 3],
    'Y': [4, 5, 6],
    'Z': [7, 8, 9]
})
result = df.drop(columns=['X', 'Z'])
print(result)
A
   Y
0  4
1  5
2  6
B
   X  Y
0  1  4
1  2  5
2  3  6
C
   X  Z
0  1  7
1  2  8
2  3  9
D
   X  Y  Z
0  1  4  7
1  2  5  8
2  3  6  9
Attempts:
2 left
💡 Hint
Use df.drop() with columns parameter to remove columns.
🔧 Debug
advanced
2:00remaining
Identify the error when dropping rows
What error will this code raise? import pandas as pd df = pd.DataFrame({'A': [1, 2, 3]}) result = df.drop(5) print(result)
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3]})
result = df.drop(5)
print(result)
AKeyError: '[5] not found in axis'
BIndexError: index 5 is out of bounds
CTypeError: drop() missing required argument 'axis'
DNo error, prints original DataFrame
Attempts:
2 left
💡 Hint
Check if the index 5 exists in the DataFrame before dropping.
🚀 Application
advanced
2:00remaining
Drop rows with missing values
Given this DataFrame, which code correctly drops all rows that have any missing (NaN) values?
Pandas
import pandas as pd
import numpy as np

df = pd.DataFrame({
    'A': [1, 2, np.nan, 4],
    'B': [np.nan, 2, 3, 4]
})
Adf.drop(columns=['A', 'B'])
Bdf.dropna(axis=1)
Cdf.dropna()
Ddf.drop([0, 1])
Attempts:
2 left
💡 Hint
Use dropna() to remove rows with missing values.
🧠 Conceptual
expert
2:00remaining
Effect of inplace parameter in drop method
What is the effect of using inplace=True in the drop method on a DataFrame?
AThe drop method ignores inplace=True and always returns a new DataFrame.
BA new DataFrame is returned with rows/columns dropped, original unchanged.
CThe method raises an error if inplace=True is used.
DThe original DataFrame is modified and the method returns None.
Attempts:
2 left
💡 Hint
inplace=True changes the original DataFrame directly.