Bird
Raised Fist0
Pandasdata~20 mins

DataFrame as labeled two-dimensional table in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
DataFrame Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
1:30remaining
What is the output of this DataFrame selection?
Given the DataFrame below, what will be the output of df.loc['b', 'Y']?
Pandas
import pandas as pd

df = pd.DataFrame({
    'X': [10, 20, 30],
    'Y': [40, 50, 60],
    'Z': [70, 80, 90]
}, index=['a', 'b', 'c'])

result = df.loc['b', 'Y']
print(result)
A80
B20
C50
DKeyError
Attempts:
2 left
💡 Hint
Remember that loc selects by label for both rows and columns.
❓ data_output
intermediate
1:30remaining
How many rows and columns does this DataFrame have?
Consider the DataFrame created below. What is the shape (rows, columns) of df?
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': range(5),
    'B': range(5, 10),
    'C': range(10, 15)
})

print(df.shape)
A(5, 3)
B(3, 5)
C(5, 5)
D(3, 3)
Attempts:
2 left
💡 Hint
Shape returns (number of rows, number of columns).
🔧 Debug
advanced
1:30remaining
What error does this code raise?
What error will this code produce when run?
Pandas
import pandas as pd

df = pd.DataFrame({'col1': [1, 2], 'col2': [3, 4]})

value = df.loc[2, 'col1']
print(value)
ANo error, prints 2
BValueError
CIndexError
DKeyError
Attempts:
2 left
💡 Hint
Check if the row label 2 exists in the DataFrame index.
❓ visualization
advanced
2:00remaining
Which plot shows the correct bar chart of column sums?
Given the DataFrame below, which option shows the correct bar chart of the sum of each column?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6],
    'C': [7, 8, 9]
})

col_sums = df.sum()

plt.bar(col_sums.index, col_sums.values)
plt.show()
ABar chart with bars at A=6, B=15, C=24
BBar chart with bars at A=3, B=5, C=7
CBar chart with bars at A=1, B=4, C=7
DLine chart with points at A=6, B=15, C=24
Attempts:
2 left
💡 Hint
Sum each column and plot those sums as bar heights.
🚀 Application
expert
2:30remaining
What is the value of 'result' after filtering and grouping?
Given the DataFrame below, what is the value of result after filtering rows where 'score' > 70 and grouping by 'team' to get the mean 'score'?
Pandas
import pandas as pd

df = pd.DataFrame({
    'team': ['A', 'B', 'A', 'B', 'C'],
    'score': [65, 85, 75, 90, 60]
})

filtered = df[df['score'] > 70]
result = filtered.groupby('team')['score'].mean()
print(result)
Ateam\nA 75.0\nB 90.0\nName: score, dtype: float64
Bteam\nA 75.0\nB 87.5\nName: score, dtype: float64
Cteam\nA 70.0\nB 87.5\nName: score, dtype: float64
Dteam\nA 70.0\nB 90.0\nName: score, dtype: float64
Attempts:
2 left
💡 Hint
Filter first, then group by team and calculate mean score.

Practice

(1/5)
1.

What is a pandas DataFrame best described as?

easy
A. A one-dimensional list of numbers
B. A type of database server
C. A function to plot graphs
D. A labeled two-dimensional table with rows and columns

Solution

  1. Step 1: Understand the structure of a DataFrame

    A DataFrame organizes data in rows and columns with labels for both, like a spreadsheet.
  2. Step 2: Compare options to definition

    Only "A labeled two-dimensional table with rows and columns" describes a labeled two-dimensional table, matching the DataFrame concept.
  3. Final Answer:

    A labeled two-dimensional table with rows and columns -> Option D
  4. Quick Check:

    DataFrame = labeled 2D table [OK]
Hint: Remember: DataFrame = table with row and column labels [OK]
Common Mistakes:
  • Confusing DataFrame with a list or array
  • Thinking DataFrame is a plotting tool
  • Mixing DataFrame with database servers
2.

Which of the following is the correct way to create a pandas DataFrame from a dictionary?

import pandas as pd

# Which line correctly creates a DataFrame?
easy
A. df = pd.DataFrame({'A' = [1, 2], 'B' = [3, 4]})
B. df = pd.DataFrame['A': [1, 2], 'B': [3, 4]]
C. df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
D. df = pd.DataFrame({'A': 1, 2, 'B': 3, 4})

Solution

  1. Step 1: Check dictionary syntax for DataFrame creation

    The dictionary keys are column names and values are lists of column data. Correct syntax uses curly braces and colons.
  2. Step 2: Identify correct pandas DataFrame constructor usage

    df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) uses pd.DataFrame with a dictionary correctly. Others have syntax errors or wrong brackets.
  3. Final Answer:

    df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) -> Option C
  4. Quick Check:

    Use pd.DataFrame with dict and colons [OK]
Hint: Use curly braces and colons inside pd.DataFrame() [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Using '=' instead of ':' in dictionary
  • Passing values without list brackets
3.

What will be the output of the following code?

import pandas as pd

data = {'Name': ['Anna', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df.loc[1, 'Name'])
medium
A. Bob
B. Anna
C. 25
D. KeyError

Solution

  1. Step 1: Understand DataFrame creation and indexing

    The DataFrame has two rows with index 0 and 1. 'Name' column has ['Anna', 'Bob'].
  2. Step 2: Access value at row index 1 and column 'Name'

    df.loc[1, 'Name'] selects row 1, column 'Name', which is 'Bob'.
  3. Final Answer:

    Bob -> Option A
  4. Quick Check:

    df.loc[1, 'Name'] = Bob [OK]
Hint: loc uses row label and column name to get value [OK]
Common Mistakes:
  • Confusing loc with iloc (positional index)
  • Expecting row 1 to be 'Anna'
  • Mistaking column name for index
4.

Identify the error in this code snippet that tries to create a DataFrame:

import pandas as pd

data = {'X': [1, 2], 'Y': [3]}
df = pd.DataFrame(data)
print(df)
medium
A. The value for key 'Y' has the wrong length (must match 'X')
B. The dictionary keys must be integers
C. pd.DataFrame cannot be created from a dictionary
D. Missing parentheses in print statement

Solution

  1. Step 1: Check dictionary values for DataFrame columns

    Each column value must be a list or array of equal length. 'Y': [3] has length 1, 'X' has length 2, which is invalid.
  2. Step 2: Identify correct error cause

    The value for key 'Y' has the wrong length (must match 'X') correctly points out the length mismatch.
  3. Final Answer:

    The value for key 'Y' has the wrong length (must match 'X') -> Option A
  4. Quick Check:

    All column values must have equal length [OK]
Hint: All DataFrame columns need list-like values of equal length [OK]
Common Mistakes:
  • Using lists of unequal lengths for columns
  • Thinking dictionary keys must be numbers
  • Assuming print needs parentheses in Python 3
5.

You have this nested dictionary representing sales data:

sales = {
  'Store1': {'Jan': 100, 'Feb': 120},
  'Store2': {'Jan': 90, 'Feb': 110}
}

How can you create a DataFrame where rows are months and columns are stores?

hard
A. df = pd.DataFrame(sales).T
B. df = pd.DataFrame.from_dict(sales, orient='index').T
C. df = pd.DataFrame(sales, orient='index')
D. df = pd.DataFrame.from_dict(sales, orient='index')

Solution

  1. Step 1: Understand the nested dictionary structure

    Keys are stores, values are dicts of months and sales. We want months as rows, stores as columns.
  2. Step 2: Use from_dict with orient='index' and transpose

    from_dict with orient='index' treats outer keys as rows. Transpose (T) swaps rows and columns to get months as rows.
  3. Final Answer:

    df = pd.DataFrame.from_dict(sales, orient='index').T -> Option B
  4. Quick Check:

    Use from_dict with orient='index' + transpose [OK]
Hint: Use from_dict with orient='index' then transpose [OK]
Common Mistakes:
  • Not transposing after from_dict
  • Using wrong orient parameter
  • Assuming pd.DataFrame(sales) arranges data correctly
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