Fix the Bug: Duplicate Column Suffixes
Intermediate · Fix the bug · from Pandas merge(), join(), concat(): Combine DataFrames Like SQL
From the lesson: Pandas merge(), join(), concat(): Combine DataFrames Like SQL
You have a table of customers and a separate table of orders. You need to match each order to the customer who placed it. If you've used SQL, you know this as a JOIN. In pandas, the same idea exists - it's called merge().
Read the lessonAfter merging, this code tries to access a column called 'value_2023' but it doesn't exist because pandas used default suffixes. Fix the merge to use custom suffixes ('_2022', '_2023') and print the 'value_2023' column as a list.
import pandas as pd
df_2022 = pd.DataFrame({'id': [1, 2, 3], 'value': [10, 20, 30]})
df_2023 = pd.DataFrame({'id': [1, 2, 3], 'value': [15, 25, 35]})
# Bug: default suffixes are _x and _y, not _2022 and _2023
result = pd.merge(df_2022, df_2023, on='id')
print(result['value_2023'].tolist())
More Pandas Merge and Join exercises
- Inner Merge Two TablesIntermediate
- Left Join to Keep All CustomersIntermediate
- Merge with Different Key NamesIntermediate
- Stack DataFrames with concatIntermediate
- Predict the Output: Outer MergeIntermediate
All Python exercises · 50 practice problems · Data Science & Machine Learning quiz