oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Left and Right Merge #389

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

Left Merge

A left merge includes all rows from the first (left) table, but only rows from the second (right) table that match the first table.

For this command, the order of the arguments matters. If the first DataFrame is company_a and we do a left join, we'll only end up with rows that appear in company_a.

By listing company_a first, we get all customers from Company A, and only customers from Company B who are also customers of Company A.

pd.merge(company_a, company_b, how='left')
oldoc63 commented 1 year ago

Right Merge

Right merge is the exact opposite of left merge. Here, the merged table will include rows from the second (right) table, but only rows from the first (left) table that match the second table.

By listing company_a first and company_b second, we get all customers from Company B, and only customers from Company A who are also customers of Company B.

pd.merge(company_a, company_b, how="right")
oldoc63 commented 1 year ago

Let's return to the two hardware stores, Store A and Store B. They're not quite sure if they want to merge into a big Super Store just yet.

Store A wants to find out what products they carry that Store B does not carry. Using a left merge, combine store_a to store_b and save the results to store_a_b_left.

The items with null in store_b_inventory are carried by Store A, but not by Store B.

Next, do the same but in reverse order, to know the items not carried by Store A, but carried by Store B.