liveBook · Manning

Merge

As explained in Starting Data Analytics with Generative AI and Python

A merge function combines tabular data structures by matching rows on one or more key columns.

merge combines tabular data by matching rows on shared key columns. A practical question might be whether each order review belongs to the expected customer. The tempting solution is to merge df_order_reviews with df_customers on customer_id using a left join, preserving every review row. section 2.6.4

Choose the key before merging

That first merge is not automatically valid: customer_id does not identify customers reliably in this data. Since order_id is the actual identifier, first merge orders with customers to obtain customer_unique_id, then merge that result into reviews on order_id. section 2.6.4

merged_df = df_order_reviews.merge(df_customers, on='customer_id', 
how='left')

Inspect the straightforward left join that adds customer_unique_id to review rows through customer_id. section 2.6.4

merged_df = df_order_reviews.merge(orders_customers[['order_id', 
'customer_unique_id']], on='order_id', how='left')

Inspect the corrected two-stage merge that carries customer_unique_id through order_id. section 2.6.4

unique_customers_per_order =
df_order_reviews.groupby('order_id')['customer_unique_id'].nunique()
multiple_customers_orders =
unique_customers_per_order[unique_customers_per_order > 1]
if len(multiple_customers_orders) > 0:
    print(f"There are {len(multiple_customers_orders)} orders with reviews 
    from multiple customers.")
    print("\nOrder IDs and the number of unique customers:")
    print(multiple_customers_orders)
else:
    print("There are no orders with reviews from multiple customers.")

Validate the result by counting distinct customers per order and identifying orders associated with multiple customers. section 2.6.4

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Demonstrates left joins, key selection, reassignment, and validation, but does not detail merge parameters beyond the join key and type.
Examples
Uses review, order, and customer DataFrames, customer_unique_id assignment, and a multiple-customer-per-order check.
What it assumes
Assumes familiarity with DataFrames, named columns, and the distinction between customer_id, customer_unique_id, and order_id.
What you can do after
Apply it via pandas to combine DataFrames, preserve review rows, and test whether the chosen key produces credible matches.
How it connects
Links DataFrames, left joins, order identifiers, customer identifiers, and groupby-based validation.

Also explained in

Compare how 5 books explain merge →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.