0
0
Pandasdata~10 mins

When to use apply vs vectorized operations in Pandas - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to calculate the square of each number in the 'values' column using a vectorized operation.

Pandas
df['squared'] = df['values'][1]2
Drag options to blanks, or click blank then click option'
A*
B**
C+
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' instead of '**' will add 2 instead of squaring.
Using '*' will multiply the column by 2, not square.
2fill in blank
medium

Complete the code to apply a custom function that doubles each value in the 'values' column using the apply method.

Pandas
df['doubled'] = df['values'].[1](lambda x: x * 2)
Drag options to blanks, or click blank then click option'
Aapply
Breduce
Cmap
Dfilter
Attempts:
3 left
💡 Hint
Common Mistakes
Using map which is similar but not the method asked here.
Using filter which is for filtering elements.
3fill in blank
hard

Fix the error in the code to correctly apply a function that returns the length of each string in the 'names' column.

Pandas
df['name_length'] = df['names'].[1](len)
Drag options to blanks, or click blank then click option'
Aapply
Bvectorize
Cmap
Dreduce
Attempts:
3 left
💡 Hint
Common Mistakes
Using vectorize which is a NumPy function, not a pandas Series method.
Using reduce which aggregates rather than applies element-wise.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that includes words longer than 4 characters and maps them to their lengths.

Pandas
{word: [1] for word in words if len(word) [2] 4}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as the value instead of its length.
Using '<' instead of '>' in the condition.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if the length is greater than 3.

Pandas
{ [1]: [2] for word in words if len(word) [3] 3 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word as key instead of uppercase.
Using '<' instead of '>' in the condition.