深度阅读

How to merge/join two DataFrames in pandas?

作者
作者
2023年08月22日
更新时间
16.99 分钟
阅读时间
0
阅读量

You can merge or join two DataFrames in pandas using the merge() function or the join() method. Here’s an example of using merge():

import pandas as pd

# Create two DataFrames
df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'value': [1, 2, 3, 4]})
df2 = pd.DataFrame({'key': ['B', 'D', 'E', 'F'], 'value': [5, 6, 7, 8]})

# Merge the two DataFrames on the 'key' column
merged_df = pd.merge(df1, df2, on='key')

print(merged_df)

This will output:

  key  value_x  value_y
0   B        2        5
1   D        4        6

In the example above, we merged the two DataFrames based on the ‘key’ column. The resulting DataFrame includes only the rows where the ‘key’ column matches in both DataFrames.

You can also use different types of joins (inner, left, right, outer) and merge on multiple columns by passing a list of column names to the on parameter.

Here is an example of using the join() method:

import pandas as pd

# Create two DataFrames
df1 = pd.DataFrame({'value': [1, 2, 3, 4]}, index=['A', 'B', 'C', 'D'])
df2 = pd.DataFrame({'value': [5, 6, 7, 8]}, index=['B', 'D', 'E', 'F'])

# Join the two DataFrames using the index of df1
joined_df = df1.join(df2, lsuffix='_df1', rsuffix='_df2')

print(joined_df)

This will output:

   value_df1  value_df2
A          1        NaN
B          2        5.0
C          3        NaN
D          4        6.0

In the example above, we used the join() method to join the two DataFrames based on their indices. The resulting DataFrame includes all rows from df1 and only the rows from df2 that have matching indices in df1.

I hope this helps! Let me know if you have any other questions

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。