Recall & Review
beginner
What does the
sample() function do in pandas?The
sample() function selects random rows from a DataFrame. It helps to get a random subset of data for analysis or testing.Click to reveal answer
beginner
How do you select 5 random rows from a DataFrame
df?Use
df.sample(n=5). This picks 5 random rows without replacement by default.Click to reveal answer
beginner
What does the
frac parameter do in sample()?The
frac parameter lets you select a fraction of rows randomly. For example, df.sample(frac=0.1) selects 10% of rows.Click to reveal answer
intermediate
How can you make the random selection reproducible using
sample()?Use the
random_state parameter with a fixed number, like df.sample(n=3, random_state=42). This ensures the same rows are picked every time.Click to reveal answer
intermediate
What happens if you set
replace=True in sample()?Setting
replace=True allows sampling with replacement. This means the same row can be picked multiple times.Click to reveal answer
Which pandas function is used to select random rows from a DataFrame?
✗ Incorrect
The
sample() function is designed to select random rows from a DataFrame.How do you select 20% of rows randomly from a DataFrame
df?✗ Incorrect
Use the
frac parameter with a decimal fraction like 0.2 to select 20% of rows.What does setting
random_state in sample() do?✗ Incorrect
Setting
random_state fixes the random seed so the same rows are selected every time.If you want to allow the same row to be picked multiple times, which parameter should you use?
✗ Incorrect
Setting
replace=True allows sampling with replacement, so rows can repeat.What is the default behavior of
sample() regarding replacement?✗ Incorrect
By default,
sample() samples without replacement, so rows are unique in the sample.Explain how to use pandas
sample() to get a random subset of rows from a DataFrame.Think about how to pick random rows and keep results consistent.
You got /4 concepts.
Describe the effect of the
replace parameter in the sample() function.Consider if the same row can appear multiple times or not.
You got /4 concepts.