Bird
Raised Fist0
Pandasdata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the drop() function do in pandas?
easy
A. Removes specified rows or columns from a DataFrame
B. Adds new rows or columns to a DataFrame
C. Sorts the DataFrame by index or column values
D. Calculates summary statistics of the DataFrame

Solution

  1. Step 1: Understand the purpose of drop()

    The drop() function is used to remove data, either rows or columns, from a DataFrame.
  2. Step 2: Identify what drop() removes

    It specifically removes rows or columns based on labels, not adding or sorting data.
  3. Final Answer:

    Removes specified rows or columns from a DataFrame -> Option A
  4. Quick Check:

    drop() removes rows/columns [OK]
Hint: Remember: drop means remove rows or columns [OK]
Common Mistakes:
  • Thinking drop adds data instead of removing
  • Confusing drop with sort or describe functions
  • Assuming drop changes data without specifying inplace
2. Which of the following is the correct syntax to drop the column named 'Age' from a DataFrame df?
easy
A. df.drop('Age', axis=0)
B. df.drop('Age', axis=1)
C. df.drop(column='Age')
D. df.drop('Age')

Solution

  1. Step 1: Identify axis for columns

    In pandas, axis=1 means columns, so to drop a column, use axis=1.
  2. Step 2: Check syntax correctness

    The syntax df.drop('Age', axis=1) correctly drops the column named 'Age'. df.drop(column='Age') is close but incorrect syntax; it should be df.drop(columns=['Age']) with a list.
  3. Final Answer:

    df.drop('Age', axis=1) -> Option B
  4. Quick Check:

    Drop column uses axis=1 [OK]
Hint: Use axis=1 to drop columns, axis=0 for rows [OK]
Common Mistakes:
  • Using axis=0 to drop columns
  • Forgetting to specify axis parameter
  • Using drop with wrong parameter names
3. Given the DataFrame df below:
   Name  Age  City
0  John   25  NY
1  Anna   30  LA
2  Mike   22  SF

What is the output of df.drop(1, axis=0)?
medium
A.
   Name  Age  City
0  John   25  NY
1  Anna   30  LA
B.
   Name  Age  City
1  Anna   30  LA
C.
   Name  Age  City
0  John   25  NY
2  Mike   22  SF
D.
   Name  Age  City
0  John   25  NY
1  Anna   30  LA
2  Mike   22  SF

Solution

  1. Step 1: Understand dropping rows by index

    Using drop(1, axis=0) removes the row with index 1, which is the row for Anna.
  2. Step 2: Identify remaining rows

    Rows with index 0 (John) and 2 (Mike) remain, so the output excludes Anna's row.
  3. Final Answer:

    Name Age City 0 John 25 NY 2 Mike 22 SF -> Option C
  4. Quick Check:

    Drop row index 1 removes Anna [OK]
Hint: Drop row by index with axis=0 [OK]
Common Mistakes:
  • Confusing axis=0 and axis=1
  • Expecting row to be removed by label instead of index
  • Not realizing drop returns a new DataFrame by default
4. What is wrong with this code snippet to drop the column 'Salary' from DataFrame df?
df.drop('Salary')
medium
A. drop() cannot remove columns
B. Should use inplace=True to drop the column
C. Column name should be in a list like ['Salary']
D. Missing axis=1 to specify dropping a column

Solution

  1. Step 1: Check default axis in drop()

    By default, drop() assumes axis=0, which means it tries to drop a row, not a column.
  2. Step 2: Specify axis=1 to drop a column

    To drop a column, you must specify axis=1. Without it, pandas looks for a row named 'Salary' and fails if none exists.
  3. Final Answer:

    Missing axis=1 to specify dropping a column -> Option D
  4. Quick Check:

    Drop columns requires axis=1 [OK]
Hint: Always specify axis=1 to drop columns [OK]
Common Mistakes:
  • Forgetting axis=1 when dropping columns
  • Assuming drop changes DataFrame without inplace=True
  • Using drop with a single string but wrong axis
5. You have a DataFrame df with columns ['Name', 'Age', 'City', 'Salary']. You want to remove the rows with index 0 and 2, and also drop the column 'Salary' in one line without changing the original DataFrame. Which code does this correctly?
hard
A. df.drop([0, 2], axis=0).drop('Salary', axis=1)
B. df.drop([0, 2, 'Salary'], axis=0)
C. df.drop(['Salary'], axis=1).drop([0, 2], axis=0, inplace=True)
D. df.drop([0, 2], axis=1).drop('Salary', axis=0)

Solution

  1. Step 1: Drop rows with index 0 and 2

    Use df.drop([0, 2], axis=0) to remove rows 0 and 2 without changing original DataFrame.
  2. Step 2: Drop column 'Salary' from the result

    Chain .drop('Salary', axis=1) to remove the column from the intermediate DataFrame.
  3. Step 3: Verify no inplace=True used

    Since we want to keep original DataFrame unchanged, do not use inplace=True. The chained drops return a new DataFrame.
  4. Final Answer:

    df.drop([0, 2], axis=0).drop('Salary', axis=1) -> Option A
  5. Quick Check:

    Chain drops with correct axis, no inplace [OK]
Hint: Chain drop with axis=0 for rows, axis=1 for columns [OK]
Common Mistakes:
  • Mixing axis values for rows and columns
  • Using inplace=True which changes original DataFrame
  • Trying to drop rows and columns in one drop call incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard