0
0
Pandasdata~20 mins

Creating new columns in Pandas - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Column Creator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of adding a new column with arithmetic operation
What is the output DataFrame after running this code?
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df['C'] = df['A'] + df['B']
print(df)
A{'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [1, 2, 3]}
B{'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [5, 7, 9]}
C{'A': [1, 2, 3], 'B': [4, 5, 6]}
D{'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [4, 5, 6]}
Attempts:
2 left
💡 Hint
Adding columns element-wise sums the values in each row.
data_output
intermediate
2:00remaining
Result of creating a column with a constant value
What is the DataFrame output after adding a new column 'D' with a constant value 10?
Pandas
import pandas as pd

df = pd.DataFrame({'X': [7, 8, 9]})
df['D'] = 10
print(df)
A{'X': [7, 8, 9]}
B{'X': [7, 8, 9], 'D': [7, 8, 9]}
C{'X': [7, 8, 9], 'D': [10, 10, 10]}
D{'X': [7, 8, 9], 'D': [0, 0, 0]}
Attempts:
2 left
💡 Hint
Assigning a single value to a new column repeats it for all rows.
🔧 Debug
advanced
2:00remaining
Identify the error when creating a new column with a list of wrong length
What error does this code raise?
Pandas
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3]})
df['B'] = [4, 5]
print(df)
AValueError: Length of values (2) does not match length of index (3)
BKeyError: 'B'
CTypeError: unsupported operand type(s) for +: 'int' and 'list'
DNo error, prints DataFrame with NaN in last row
Attempts:
2 left
💡 Hint
The list assigned must match the number of rows exactly.
🚀 Application
advanced
2:00remaining
Create a new column based on condition from existing column
Given a DataFrame with column 'score', which option creates a new column 'passed' that is True if score >= 50, else False?
Pandas
import pandas as pd

df = pd.DataFrame({'score': [45, 55, 60, 30]})
Adf['passed'] = df['score'] > 50
Bdf['passed'] = df['score'] < 50
Cdf['passed'] = df['score'] == 50
Ddf['passed'] = df['score'] >= 50
Attempts:
2 left
💡 Hint
Use comparison operator >= to include 50 as passing.
visualization
expert
3:00remaining
Visualize the effect of creating a new column with a function
Which option shows the correct bar chart after creating a new column 'length' with the length of strings in column 'words'?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'words': ['apple', 'banana', 'pear']})
df['length'] = df['words'].apply(len)
df.plot.bar(x='words', y='length')
plt.show()
ABar chart with 'words' on x-axis and bars showing lengths [5, 6, 4]
BBar chart with 'words' on x-axis and bars showing lengths [6, 5, 4]
CLine chart with 'words' on x-axis and lengths on y-axis
DScatter plot with 'words' and 'length' values
Attempts:
2 left
💡 Hint
The length of 'apple' is 5, 'banana' is 6, 'pear' is 4.