0
0
Pandasdata~30 mins

Merging on different column names in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Merging DataFrames on Different Column Names
📖 Scenario: You work in a small store. You have two lists of information. One list has product IDs and names. The other list has product codes and prices. You want to combine these lists to see the product name with its price.
🎯 Goal: Build a program that merges two pandas DataFrames using columns with different names to get a combined table showing product names and prices.
📋 What You'll Learn
Create two pandas DataFrames with given data
Use a merge operation on columns with different names
Display the merged DataFrame with product names and prices
💡 Why This Matters
🌍 Real World
Stores and businesses often have data in separate lists or tables with different column names. Merging on different column names helps combine this data for better analysis.
💼 Career
Data analysts and scientists frequently merge datasets from different sources. Knowing how to merge on different column names is a key skill for data cleaning and preparation.
Progress0 / 4 steps
1
Create the first DataFrame
Create a pandas DataFrame called df_products with these exact columns and values:
product_id: [101, 102, 103]
product_name: ['Pen', 'Notebook', 'Eraser']
Pandas
Need a hint?

Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.

2
Create the second DataFrame
Create a pandas DataFrame called df_prices with these exact columns and values:
code: [101, 102, 103]
price: [1.5, 2.0, 0.5]
Pandas
Need a hint?

Use pd.DataFrame with a dictionary for the second table, matching the exact column names and values.

3
Merge the DataFrames on different column names
Use pd.merge to merge df_products and df_prices where df_products.product_id matches df_prices.code. Save the result in a variable called df_merged.
Pandas
Need a hint?

Use pd.merge with left_on and right_on parameters to join on columns with different names.

4
Display the merged DataFrame
Print the df_merged DataFrame to show the combined product names and prices.
Pandas
Need a hint?

Use print(df_merged) to show the combined table.