Challenge - 5 Problems
Pipe Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of method chaining with pipe
What is the output of this code using
pipe for method chaining on a pandas DataFrame?Data Analysis Python
import pandas as pd def add_one(df): return df + 1 def multiply_two(df): return df * 2 df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) result = df.pipe(add_one).pipe(multiply_two) print(result)
Attempts:
2 left
💡 Hint
Remember that
pipe passes the DataFrame to the function and returns the result, allowing chaining.✗ Incorrect
First,
add_one adds 1 to each element, so the DataFrame becomes [[2,4],[3,5]]. Then multiply_two multiplies each element by 2, resulting in [[4,8],[6,10]].❓ data_output
intermediate2:00remaining
Resulting DataFrame shape after pipe chaining
Given the following code, what is the shape of the resulting DataFrame?
Data Analysis Python
import pandas as pd def filter_even(df): return df[df['num'] % 2 == 0] def add_square(df): df['square'] = df['num'] ** 2 return df df = pd.DataFrame({'num': [1, 2, 3, 4, 5]}) result = df.pipe(filter_even).pipe(add_square) print(result.shape)
Attempts:
2 left
💡 Hint
Check how many numbers are even and how many columns are in the final DataFrame.
✗ Incorrect
The filter keeps only even numbers: 2 and 4, so 2 rows. The
add_square function adds a new column, so total 2 columns.🔧 Debug
advanced2:00remaining
Identify the error in pipe chaining
What error will this code raise when using
pipe for method chaining?Data Analysis Python
import pandas as pd def add_column(df): df['new'] = df['A'] + 1 return df def wrong_func(df, x): return df + x df = pd.DataFrame({'A': [1, 2]}) result = df.pipe(add_column).pipe(wrong_func) print(result)
Attempts:
2 left
💡 Hint
Check the definition of
wrong_func and how pipe passes arguments.✗ Incorrect
The
pipe method passes only the DataFrame by default. wrong_func requires two arguments but only one is given, causing a TypeError about missing argument 'x'.🚀 Application
advanced2:00remaining
Using pipe to apply multiple transformations
Which option correctly uses
pipe to apply these transformations in order: drop rows with NaN, reset index, and rename column 'old' to 'new'?Data Analysis Python
import pandas as pd def drop_na(df): return df.dropna() def reset_idx(df): return df.reset_index(drop=True) def rename_col(df): return df.rename(columns={'old': 'new'}) df = pd.DataFrame({'old': [1, None, 3], 'val': [4, 5, None]})
Attempts:
2 left
💡 Hint
Think about the order: first remove NaNs, then reset index, then rename columns.
✗ Incorrect
Dropping NaNs first removes rows with missing data. Resetting index after ensures index is continuous. Renaming columns last changes column names correctly.
🧠 Conceptual
expert2:00remaining
Why use pipe in method chaining?
Which statement best explains the main advantage of using
pipe in pandas method chaining?Attempts:
2 left
💡 Hint
Think about how
pipe helps when using your own functions in a chain.✗ Incorrect
pipe passes the DataFrame as the first argument to any function, making it easy to chain custom functions without breaking the flow.