📖 Scenario: You work in a small bookstore. You have a list of books with their prices and want to randomly pick some books to check their condition.
🎯 Goal: Learn how to use the sample() method in pandas to select random rows from a dataset.
📋 What You'll Learn
Create a pandas DataFrame with book data
Set a variable for the number of random rows to select
Use the sample() method to pick random rows
Print the selected random rows
💡 Why This Matters
🌍 Real World
Random sampling helps quality check a few items from a large list without checking everything.
💼 Career
Data scientists often use random sampling to explore data or create training and testing sets.
Progress0 / 4 steps
1
Create the books DataFrame
Import pandas as pd and create a DataFrame called books with these exact entries: 'Title' as ['The Alchemist', '1984', 'To Kill a Mockingbird', 'The Great Gatsby', 'Moby Dick'], and 'Price' as [10.99, 8.99, 12.5, 9.75, 11.0].
Data Analysis Python
Hint
Use pd.DataFrame() with a dictionary where keys are column names and values are lists of data.
2
Set the number of random rows to select
Create a variable called num_samples and set it to 2 to select two random rows.
Data Analysis Python
Hint
Just assign the number 2 to the variable num_samples.
3
Select random rows using sample()
Use the sample() method on the books DataFrame with the argument n=num_samples and save the result in a variable called random_books.
Data Analysis Python
Hint
Call books.sample(n=num_samples) and assign it to random_books.
4
Print the random rows
Print the variable random_books to display the randomly selected rows.
Data Analysis Python
Hint
Use print(random_books) to show the selected rows. The exact rows will vary because of randomness.
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
Step 1: Understand the purpose of sample()
The sample() function is designed to pick random rows from a DataFrame.
Step 2: Compare with other options
Sorting, removing duplicates, and grouping are different operations and not related to sample().
Final Answer:
It selects random rows from the DataFrame. -> Option C
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
Step 1: Recall sample() parameters
The correct parameter to specify number of rows is n, so df.sample(n=5) is correct.
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.
Final Answer:
df.sample(n=5) -> Option A
Quick Check:
Use n= to specify row count [OK]
Hint: Use n=5 to specify number of rows in sample() [OK]
Setting random_state=1 makes sampling reproducible, so the same rows are picked every time.
Step 2: Check which rows are picked
Running the code picks rows with index 1 and 4 (values 20 and 50).
Final Answer:
Rows with index 1 and 4 -> Option A
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
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.
Step 2: Identify the error
Using both 3 (positional for n) and frac=0.5 causes an error.
Final Answer:
You cannot use both n and frac together. -> Option B
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
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.
Step 2: Check random_state for reproducibility
Setting random_state=42 ensures the sample is the same every run.
Step 3: Identify incorrect options
frac=10 is invalid (fraction >1), and n=0.1 is invalid because n must be integer.
Final Answer:
df.sample(frac=0.1, random_state=42) -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.