0
0
Data Analysis Pythondata~20 mins

Adding and removing columns in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Column Mastery Badge
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 a calculation
What is the output DataFrame after running this code that adds a new column 'C' as the sum of columns 'A' and 'B'?
Data Analysis Python
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': [5, 7, 9]}
B{'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [4, 5, 6]}
C{'A': [1, 2, 3], 'B': [4, 5, 6]}
DSyntaxError
Attempts:
2 left
💡 Hint
Adding columns can be done by simple arithmetic on existing columns.
data_output
intermediate
2:00remaining
Result after removing a column using drop
What is the DataFrame output after dropping column 'B' from this DataFrame?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [10, 20], 'B': [30, 40], 'C': [50, 60]})
df2 = df.drop('B', axis=1)
print(df2)
AKeyError
B{'B': [30, 40], 'C': [50, 60]}
C{'A': [10, 20], 'C': [50, 60]}
D{'A': [10, 20], 'B': [30, 40], 'C': [50, 60]}
Attempts:
2 left
💡 Hint
drop with axis=1 removes columns.
🔧 Debug
advanced
2:00remaining
Identify the error when removing a column
What error does this code raise when trying to remove a column 'D' that does not exist?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'X': [1, 2], 'Y': [3, 4]})
df.drop('D', axis=1)
AKeyError
BValueError
CAttributeError
DNo error, returns original DataFrame
Attempts:
2 left
💡 Hint
Dropping a non-existing column without errors raises a specific exception.
🚀 Application
advanced
2:00remaining
Add a column based on condition from another column
Which option correctly adds a new column 'Status' that is 'High' if column 'Score' is above 50, otherwise 'Low'?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'Score': [45, 55, 60, 30]})
Adf['Status'] = ['High' if x > 50 else 'Low' for x in df['Score']]
Bdf['Status'] = df['Score'].map({'High': x > 50, 'Low': x <= 50})
Cdf['Status'] = df['Score'] > 50
Ddf['Status'] = df['Score'].apply(lambda x: 'High' if x > 50 else 'Low')
Attempts:
2 left
💡 Hint
Use apply with a lambda for conditional assignment.
🧠 Conceptual
expert
2:00remaining
Understanding inplace parameter effect when removing columns
What is the effect of using inplace=True in df.drop('A', axis=1, inplace=True)?
AThe column 'A' is removed and a new DataFrame without 'A' is returned.
BThe column 'A' is removed from df and the method returns None.
CThe column 'A' is removed but df remains unchanged.
DThe method raises an error because inplace=True is invalid.
Attempts:
2 left
💡 Hint
inplace=True modifies the original DataFrame directly.