Python para Data Science e Analytics
Python para Data Science e Analytics › Guides › Left Join in pandas: How to Keep Every Row from the Left Tab

Left Join in pandas: How to Keep Every Row from the Left Table

A left join in pandas is written as pd.merge(left, right, how='left') and keeps every row from the left DataFrame. Rows from the right are added only when the join key matches; otherwise the right-side columns contain NaN. Use it when the left table is your main record set and you do not want to lose any of its rows.

Topics: pandas, merge, sql

What does a left join in pandas do, and why does it keep the left table?

In pandas, a left join returns all rows from the left DataFrame and the matching rows from the right DataFrame. The join is controlled by how='left' in pd.merge. If a left row has no match on the right, the right-side columns are filled with NaN.

This differs from an inner join, which keeps only rows with a match on both sides. In an orders table, a left join keeps every order and adds customer details only when the customer record exists.

A left join does not invent rows. It can, however, repeat a left row when the right table has more than one row with the same key. Check key uniqueness before joining.

How do you run a left join in pandas with pd.merge?

The most common form is pd.merge(left, right, on='key', how='left'). When the key has the same name in both DataFrames, pass it in on.

import pandas as pd orders = pd.DataFrame({ 'order_id': [1, 2, 3], 'customer_id': [10, 11, 99], 'amount': [120.0, 80.0, 50.0] }) customers = pd.DataFrame({ 'customer_id': [10, 11], 'name': ['Ana', 'Bruno'] }) result = pd.merge(orders, customers, on='customer_id', how='left') print(result)

Order 3 stays in the result because orders is the left DataFrame. Customer 99 is missing from customers, so the name column is NaN for that row.

When the key names differ, use left_on and right_on. For example: pd.merge(orders, customers, left_on='customer_id', right_on='client_id', how='left').

Which pd.merge parameters control a left join?

on names the key column shared by both DataFrames. left_on and right_on handle different key names. suffixes sets the suffixes for overlapping column names; the default is ('_x', '_y').

indicator adds a _merge column. With how='left', the values are 'both' for a match and 'left_only' for a left row with no match. validate checks the expected cardinality, such as 'many_to_one' or 'one_to_one', and raises an error when the data does not fit.

These parameters do not change the left-join rule, but they change how easy it is to audit the result.

When should you use a left join instead of an inner or outer join?

Use a left join when the left table is the base of the analysis and every record in it matters. Examples include an order list that receives customer data, a user list that receives plan data, or a transaction list that receives merchant category data.

Use an inner join when you only want records that exist on both sides. Use an outer join when you need to keep unmatched rows from both DataFrames. The choice should follow the analysis question, not habit.

Before joining, compare row counts and check whether the key is unique. A left join with a non-unique key on the right can increase the row count and change averages, sums, and counts.

What are common left join errors in pandas, and how do you avoid them?

The most common error is a non-unique key in the right table. If two customer rows share customer_id 10, every left row with customer_id 10 appears twice. The result may look valid, but row counts and aggregates are wrong.

A second error is a data type mismatch. If customer_id is an integer in one table and a string in the other, pandas may not match the values as expected. Convert both keys to the same type before merging.

A third error is ignoring NaN values after the join. Decide whether unmatched rows should be kept, filtered, or filled. If you filter them out later, an inner join may have expressed the intent more clearly.

Use indicator=True and validate to catch these issues early. Also check the suffixes when both tables have columns with the same name.

How can you practice left joins and pandas for analytics?

Left joins are part of everyday data preparation, and they are easier to learn with guided practice. The Python for Data Science and Analytics course covers NumPy and Pandas, data cleaning and preparation, outlier detection, visualization, and a final end-to-end project with exploratory analysis and machine learning.

The course has three main modules — Basic, Intermediate, and Advanced — plus bonus modules. It starts from zero and does not require prior programming experience.

The course is recorded, self-paced, and includes guided exercises, real market projects for a portfolio, a certificate of completion, an exclusive community for networking, and unlimited access with no expiration date. The price is R$ 470, or 12 installments of R$ 20.37 on the card, through the Hotmart checkout, which also supports boleto. You can start at python.paanalytics.net or go directly to the checkout.

Join typeRows keptMissing matches
leftAll rows from the left DataFrameRight-side columns become NaN
innerRows with a key match on both sidesRows without a match are dropped
outerRows from both DataFramesColumns from either side can be NaN
rightAll rows from the right DataFrameLeft-side columns become NaN

Join behavior reflects pandas pd.merge how options; this table is not a product comparison.

Want to practice left joins and pandas in a complete course from zero to advanced?

Secure payment via Hotmart, up to 12 installments.

Enroll now Product site

Frequently asked questions

Does a left join keep rows with no match on the right?

Yes. With how='left', every row from the left DataFrame appears in the result. When the key has no match on the right, the right-side columns are filled with NaN.

What is the difference between a left join and an inner join in pandas?

An inner join keeps only rows with a key match on both sides. A left join keeps every row from the left DataFrame and adds right-side data only where a match exists.

How do I find unmatched rows after a left join?

Use indicator=True in pd.merge. Then filter the _merge column for 'left_only' to see the left rows that had no match in the right DataFrame.

Why did my left join create duplicate rows?

The right table probably has more than one row with the same join key. Check the right key with duplicated() and remove duplicates or use validate to confirm the expected cardinality.

Can I left join on more than one column in pandas?

Yes. Pass a list to on, such as on=['customer_id', 'year'], or use left_on and right_on with matching lists when the column names differ.

Ler em português →