Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Sample() for random rows in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sample() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this sample() code?
Given the DataFrame df below, what will df.sample(n=3, random_state=1) return?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [10, 20, 30, 40, 50], 'B': ['a', 'b', 'c', 'd', 'e']})
sample_df = df.sample(n=3, random_state=1)
print(sample_df)
A
   A  B
2  30  c
0  10  a
1  20  b
B
   A  B
1  20  b
4  50  e
2  30  c
C
   A  B
4  50  e
0  10  a
3  40  d
D
   A  B
3  40  d
1  20  b
0  10  a
Attempts:
2 left
💡 Hint
Remember that setting random_state fixes the random selection order.
❓ data_output
intermediate
1:00remaining
How many rows are returned by sample() with frac=0.4?
If a DataFrame has 10 rows, what is the number of rows returned by df.sample(frac=0.4, random_state=5)?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'X': range(10)})
sample_df = df.sample(frac=0.4, random_state=5)
print(len(sample_df))
A6
B4
C3
D5
Attempts:
2 left
💡 Hint
frac means fraction of total rows.
🔧 Debug
advanced
1:30remaining
What error does this sample() code raise?
What error will this code produce?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3]})
sample_df = df.sample(n=5)
AKeyError: 'n'
BTypeError: sample() got an unexpected keyword argument 'n'
CNo error, returns 5 rows with NaNs
DValueError: Cannot take a larger sample than population when 'replace=False'
Attempts:
2 left
💡 Hint
Check if sample size is larger than DataFrame size without replacement.
🚀 Application
advanced
1:30remaining
Which code produces a random sample with replacement?
You want to randomly select 4 rows from a DataFrame of 3 rows, allowing repeats. Which code does this?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3]})
Adf.sample(frac=1.33, random_state=0)
Bdf.sample(n=4, replace=False, random_state=0)
Cdf.sample(n=4, replace=True, random_state=0)
Ddf.sample(n=4)
Attempts:
2 left
💡 Hint
Sampling with replacement allows repeats and can exceed original size.
🧠 Conceptual
expert
1:00remaining
What is the effect of setting random_state in sample()?
Why do we set the random_state parameter in df.sample()?
ATo ensure the sample is the same every time the code runs
BTo increase the sample size automatically
CTo speed up the sampling process
DTo sort the sampled rows by their index
Attempts:
2 left
💡 Hint
Think about reproducibility in random operations.

Practice

(1/5)
1. What does the sample() function do when used on a DataFrame in pandas?
easy
A. It groups the DataFrame by a column.
B. It sorts the DataFrame by index.
C. It selects random rows from the DataFrame.
D. It removes duplicate rows from the DataFrame.

Solution

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

    The sample() function is designed to pick random rows from a DataFrame.
  2. Step 2: Compare with other options

    Sorting, removing duplicates, and grouping are different operations and not related to sample().
  3. Final Answer:

    It selects random rows from the DataFrame. -> Option C
  4. Quick Check:

    sample() = random rows [OK]
Hint: Remember: sample() picks random rows only [OK]
Common Mistakes:
  • Confusing sample() with sort()
  • Thinking sample() removes duplicates
  • Assuming sample() groups data
2. Which of the following is the correct syntax to get 5 random rows from a DataFrame named df?
easy
A. df.sample(n=5)
B. df.sample(frac=5)
C. df.sample(rows=5)
D. df.sample(size=5)

Solution

  1. Step 1: Recall sample() parameters

    The correct parameter to specify number of rows is n, so df.sample(n=5) is correct.
  2. Step 2: Check other options

    df.sample(frac=5) is invalid because frac must be between 0 and 1; rows and size are invalid parameters.
  3. Final Answer:

    df.sample(n=5) -> Option A
  4. Quick Check:

    Use n= to specify row count [OK]
Hint: Use n=5 to specify number of rows in sample() [OK]
Common Mistakes:
  • Using invalid parameters like rows or size
  • Forgetting to use n= when specifying count
  • Using frac with count value instead of fraction
3. Given the DataFrame df below:
import pandas as pd
df = pd.DataFrame({'A': [10, 20, 30, 40, 50]})
sample_df = df.sample(n=2, random_state=1)

What will sample_df contain?
medium
A. Rows with index 1 and 4
B. Rows with index 2 and 4
C. Rows with index 0 and 3
D. Rows with index 0 and 1

Solution

  1. Step 1: Understand random_state effect

    Setting random_state=1 makes sampling reproducible, so the same rows are picked every time.
  2. Step 2: Check which rows are picked

    Running the code picks rows with index 1 and 4 (values 20 and 50).
  3. Final Answer:

    Rows with index 1 and 4 -> Option A
  4. Quick Check:

    random_state=1 fixes sample rows [OK]
Hint: Use random_state to get same sample every time [OK]
Common Mistakes:
  • Ignoring random_state and expecting different rows
  • Assuming sample picks first rows always
  • Confusing index with row position
4. What is wrong with this code snippet to get 3 random rows from df?
sampled = df.sample(3, random_state=42, frac=0.5)
medium
A. The sample size must be greater than the DataFrame length.
B. You cannot use both n and frac together.
C. random_state must be an integer greater than 100.
D. The code is correct and will run without errors.

Solution

  1. Step 1: Understand sample() parameters

    You can specify either n (number of rows) or frac (fraction of rows), but not both at the same time.
  2. Step 2: Identify the error

    Using both 3 (positional for n) and frac=0.5 causes an error.
  3. Final Answer:

    You cannot use both n and frac together. -> Option B
  4. Quick Check:

    Use either n or frac, not both [OK]
Hint: Use only n or frac in sample(), never both [OK]
Common Mistakes:
  • Trying to use n and frac together
  • Thinking random_state must be >100
  • Assuming sample size can exceed DataFrame length
5. You have a DataFrame df with 1000 rows. You want to randomly select 10% of the rows but ensure the sample is the same every time you run the code. Which code snippet correctly does this?
hard
A. df.sample(n=0.1, random_state=42)
B. df.sample(n=100)
C. df.sample(frac=10, random_state=42)
D. df.sample(frac=0.1, random_state=42)

Solution

  1. Step 1: Understand fraction sampling

    To select 10% of rows, use frac=0.1. Using n=100 selects fixed rows (not percentage-based) and df.sample(n=100) lacks random_state so not reproducible every time.
  2. Step 2: Check random_state for reproducibility

    Setting random_state=42 ensures the sample is the same every run.
  3. Step 3: Identify incorrect options

    frac=10 is invalid (fraction >1), and n=0.1 is invalid because n must be integer.
  4. Final Answer:

    df.sample(frac=0.1, random_state=42) -> Option D
  5. Quick Check:

    Use frac=0.1 and random_state for fixed 10% sample [OK]
Hint: Use frac=0.1 and random_state for fixed 10% sample [OK]
Common Mistakes:
  • Using frac > 1 instead of fraction
  • Passing float to n instead of int
  • Not setting random_state for reproducibility
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