0
0
Pandasdata~20 mins

apply() on rows (axis=1) in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Apply Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of apply() with axis=1 on a DataFrame
What is the output of this code snippet?
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

result = df.apply(lambda row: row['A'] + row['B'], axis=1)
print(result)
Pandas
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3],
    'B': [4, 5, 6]
})

result = df.apply(lambda row: row['A'] + row['B'], axis=1)
print(result)
A
A    6
B   15
dtype: int64
B
0    4
1    5
2    6
dtype: int64
C
0    5
1    7
2    9
dtype: int64
D
0    1
1    2
2    3
dtype: int64
Attempts:
2 left
💡 Hint
Remember that axis=1 means the function is applied to each row, not each column.
data_output
intermediate
2:00remaining
Resulting DataFrame after apply() with axis=1
Given this code, what is the resulting DataFrame?
import pandas as pd

df = pd.DataFrame({
    'X': [10, 20],
    'Y': [1, 2]
})

def add_columns(row):
    return pd.Series({'Sum': row['X'] + row['Y'], 'Diff': row['X'] - row['Y']})

result = df.apply(add_columns, axis=1)
print(result)
Pandas
import pandas as pd

df = pd.DataFrame({
    'X': [10, 20],
    'Y': [1, 2]
})

def add_columns(row):
    return pd.Series({'Sum': row['X'] + row['Y'], 'Diff': row['X'] - row['Y']})

result = df.apply(add_columns, axis=1)
print(result)
A
   Sum  Diff
0   11     9
1   22    18
B
   X   Y
0  11   9
1  22  18
C
   Sum  Diff
0   9    11
1  18    22
D
0    11
1    22
dtype: int64
Attempts:
2 left
💡 Hint
The function returns a Series with two keys, so the result will have two columns.
🔧 Debug
advanced
2:00remaining
Identify the error in apply() usage with axis=1
What error does this code raise?
import pandas as pd

df = pd.DataFrame({'A': [1, 2]})

result = df.apply(lambda row: row['B'] * 2, axis=1)
print(result)
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2]})

result = df.apply(lambda row: row['B'] * 2, axis=1)
print(result)
AKeyError: 'B'
BTypeError: unsupported operand type(s) for *: 'int' and 'str'
CAttributeError: 'Series' object has no attribute 'B'
DNo error, outputs a Series with doubled values
Attempts:
2 left
💡 Hint
Check if the column 'B' exists in the DataFrame.
visualization
advanced
2:00remaining
Visualizing apply() results on rows
What does this code plot?
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'Height': [150, 160, 170],
    'Weight': [50, 60, 70]
})

df['BMI'] = df.apply(lambda row: row['Weight'] / ((row['Height']/100) ** 2), axis=1)

plt.bar(df.index, df['BMI'])
plt.xlabel('Person Index')
plt.ylabel('BMI')
plt.title('BMI of Persons')
plt.show()
Pandas
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({
    'Height': [150, 160, 170],
    'Weight': [50, 60, 70]
})

df['BMI'] = df.apply(lambda row: row['Weight'] / ((row['Height']/100) ** 2), axis=1)

plt.bar(df.index, df['BMI'])
plt.xlabel('Person Index')
plt.ylabel('BMI')
plt.title('BMI of Persons')
plt.show()
AA line chart showing Height vs Weight
BA bar chart showing BMI values for 3 persons indexed 0,1,2
CA scatter plot of Height vs BMI
DAn empty plot with no bars
Attempts:
2 left
💡 Hint
Look at the plt.bar call and what data it uses.
🚀 Application
expert
2:30remaining
Using apply() with axis=1 to classify data
Given this DataFrame, which option correctly adds a new column 'Category' that labels each row as 'High' if 'Score' > 75, else 'Low'?
import pandas as pd

df = pd.DataFrame({'Name': ['Anna', 'Bob', 'Cara'], 'Score': [80, 70, 90]})

# Add 'Category' column here
Pandas
import pandas as pd

df = pd.DataFrame({'Name': ['Anna', 'Bob', 'Cara'], 'Score': [80, 70, 90]})

# Add 'Category' column here
Adf['Category'] = df.apply(lambda row: 'High' if row.Score > 75 else 'Low')
Bdf['Category'] = df['Score'].apply(lambda x: 'High' if x > 75 else 'Low')
Cdf['Category'] = df.apply(lambda col: 'High' if col['Score'] > 75 else 'Low', axis=0)
Ddf['Category'] = df.apply(lambda row: 'High' if row['Score'] > 75 else 'Low', axis=1)
Attempts:
2 left
💡 Hint
Remember axis=1 applies the function to each row, axis=0 applies to columns.