Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to select 3 random rows from the DataFrame df.
Data Analysis Python
random_rows = df.[1](3)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
head() or tail() which select fixed rows, not random ones.Using
sort() which orders rows but does not sample.✗ Incorrect
The
sample() method selects random rows from a DataFrame. Here, df.sample(3) returns 3 random rows.2fill in blank
mediumComplete the code to select 20% random rows from the DataFrame df.
Data Analysis Python
random_rows = df.sample(frac=[1]) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 20 instead of 0.2, which causes an error.
Using 2 which is greater than 1 and invalid for fraction.
✗ Incorrect
The
frac parameter in sample() specifies the fraction of rows to return. frac=0.2 means 20% of rows.3fill in blank
hardFix the error in the code to select 5 random rows without replacement from df.
Data Analysis Python
random_rows = df.sample(n=5, replace=[1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting
replace=True causes repeated rows.Using
None or 0 which are invalid for replace.✗ Incorrect
To sample without replacement, set
replace=False. This means rows are not repeated.4fill in blank
hardFill both blanks to select 4 random rows with a fixed random seed for reproducibility.
Data Analysis Python
random_rows = df.sample(n=[1], random_state=[2])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
random_state=0 which is valid but less common than 42.Mixing up
n and random_state values.✗ Incorrect
The
n parameter sets the number of rows to sample. random_state fixes the seed so results are repeatable.5fill in blank
hardFill all three blanks to create a dictionary of word lengths for words longer than 3 letters.
Data Analysis Python
lengths = {word: [1] for word in words if len(word) [2] [3] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
word instead of len(word) for the value.Using
< or == instead of > in the condition.✗ Incorrect
The dictionary comprehension maps each word to its length if the word length is greater than 3.