🐍 Data Analyst Interview Series: Part 31
Guys, let's continue our Python Interview Series.
In the previous part, we covered Pandas GroupBy and Aggregation. Today, let's focus on combining datasets and reshaping data using Pandas.
Part 31, Pandas Merge, Join, Concat & Pivot Tables
1️⃣ What is the difference between merge() and concat() in Pandas?
Sample Answer:
Both functions combine DataFrames, but they work differently.
merge() combines DataFrames based on common columns or indexes, similar to SQL JOIN operations.
Example:
import pandas as pd
df1 = pd.DataFrame({
"CustomerID": [1, 2, 3],
"Name": ["Rahul", "Priya", "Amit"]
})
df2 = pd.DataFrame({
"CustomerID": [1, 2, 3],
"Sales": [5000, 7000, 6000]
})
result = pd.merge(df1, df2, on="CustomerID")
concat() combines DataFrames along rows or columns.
result = pd.concat([df1, df2], axis=0)
In simple terms:
Merge → Combine based on matching keys.
Concat → Stack DataFrames along rows or columns.
2️⃣ What are the different types of joins in Pandas?
Pandas supports different types of joins through the merge() function.
• Inner Join, Returns matching records from both DataFrames.
• Left Join, Returns all records from the left DataFrame and matching records from the right.
• Right Join, Returns all records from the right DataFrame and matching records from the left.
• Outer Join, Returns all records from both DataFrames.
result = pd.merge(
df1,
df2,
on="CustomerID",
how="left"
)
This keeps all customers from df1, even if they don't have matching records in df2.
These joins are useful when combining customer, sales, product, and transaction datasets.
3️⃣ What is the difference between join() and merge()?
merge() is primarily used to combine DataFrames based on columns or indexes.
join() is primarily used to combine DataFrames using their indexes by default.
result = df1.join(
df2,
how="left"
)
We can also specify columns for joining using join().
I generally use merge() when joining datasets using common business keys such as CustomerID or ProductID.
4️⃣ What happens when both DataFrames contain columns with the same name?
When merging DataFrames with overlapping non-key column names, Pandas automatically adds suffixes.
result = pd.merge(
df1,
df2,
on="CustomerID",
suffixes=("_left", "_right")
)
This helps distinguish columns coming from different DataFrames.
It is useful when combining datasets that contain common fields such as Status, Date, or Amount.
5️⃣ What is a Pivot Table in Pandas?
A Pivot Table is used to summarize and reorganize data based on different categories.
It is similar to PivotTables in Excel.
df = pd.DataFrame({
"Region": ["North", "South", "North", "South"],
"Product": ["Laptop", "Laptop", "Mobile", "Mobile"],
"Sales": [50000, 60000, 30000, 40000]
})
pivot = pd.pivot_table(
df,
values="Sales",
index="Region",
columns="Product",
aggfunc="sum",
fill_value=0
)
print(pivot)
This creates a summary of sales by region and product.
6️⃣ What is the difference between pivot() and pivot_table()?
pivot() reshapes data but does not perform aggregation. It requires each index-column combination to be unique.
pivot_table() can handle duplicate combinations by applying an aggregation function such as sum, mean, or count.
df.pivot(
index="Region",
columns="Product",
values="Sales"
)
Using pivot_table():
df.pivot_table(
index="Region",
columns="Product",
values="Sales",
aggfunc="sum"
)