📜  pandas left join - Python (1)

📅  最后修改于: 2023-12-03 15:03:28.422000             🧑  作者: Mango

Pandas Left Join - Python

Pandas is a common data analysis tool in Python that offers easy and intuitive data manipulation capabilities. Left join is one of the methods for combining data from two or more tables based on a common attribute.

Syntax
left_df.merge(right_df, on='common_attribute', how='left')

The left_df and right_df are the dataframes to merge. The on parameter specifies the common attribute to merge the dataframes on. The how parameter specifies the type of join. In this case, how='left' specifies a left join.

Example

Suppose we have two dataframes, df1 and df2:

import pandas as pd

df1 = pd.DataFrame({'ID': [1, 2, 3, 4], 'Name': ['John', 'Mary', 'Peter', 'David']})
df2 = pd.DataFrame({'ID': [2, 3, 6], 'Age': [25, 30, 35]})
df1:
   ID   Name
0   1   John
1   2   Mary
2   3   Peter
3   4   David

df2:
   ID   Age
0   2   25
1   3   30
2   6   35

We can perform a left join on df1 and df2 based on the ID attribute as follows:

left_join_df = df1.merge(df2, on='ID', how='left')
left_join_df:
   ID   Name   Age
0   1   John   NaN
1   2   Mary   25.0
2   3   Peter  30.0
3   4   David  NaN

The resulting dataframe has all the entries from df1 and only the matched entries from df2. For the unmatched entries in df1, the columns from df2 are filled with NaN values.

Conclusion

In this post, we discussed how to perform a left join in pandas using the merge() function. Left join is a powerful tool for merging data from different sources and can be used in a variety of data analysis tasks.