0
0
Pandasdata~10 mins

Why custom functions matter in Pandas - Test Your Understanding

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

Complete the code to create a simple function that doubles a number.

Pandas
def double(x):
    return x [1] 2

result = double(5)
print(result)
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 doubling.
2fill in blank
medium

Complete the code to apply a custom function to a pandas DataFrame column.

Pandas
import pandas as pd

def add_five(n):
    return n + 5

df = pd.DataFrame({'num': [1, 2, 3]})
df['new_num'] = df['num'].[1](add_five)
print(df)
Drag options to blanks, or click blank then click option'
Aapplymap
Bapply
Cmap
Dtransform
Attempts:
3 left
💡 Hint
Common Mistakes
Using applymap which works on DataFrames, not Series.
3fill in blank
hard

Fix the error in the custom function that should return the square of a number.

Pandas
def square(x):
    return x [1] x

print(square(4))
Drag options to blanks, or click blank then click option'
A*
B+
C-
D//
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+' will add the number to itself, not square it.
4fill in blank
hard

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

Pandas
words = ['apple', 'bat', 'car', 'door']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B<
C>
Dword
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' will select words with length less than 3.
5fill in blank
hard

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

Pandas
words = ['apple', 'bat', 'car', 'door']
result = { [1]: [2] for word in words if len(word) [3] 3 }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
C>
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of uppercase.