怎样Join Pandas dataframes?

怎样像SQL一样把两个表(Dataframe) join在一起

select
  df1.*,
  df2.col3
from
  df1
left join
  df2
on 
  df1.col1 = df2.col1

可以用dataframe里的merge method

import pandas as pd

# Sample data in df1 and df2
data1 = {'col1': ['A', 'B', 'C'],
         'col2': [100, 200, 300]}
df1 = pd.DataFrame(data1)

data2 = {'col1': ['A1', 'A2', 'A3'],
         'col3': [150, 250, 350]}
df2 = pd.DataFrame(data2)

# Perform a left join on "symbol" column
result_df = df1.merge(df2, on='col1', how='left')

print(result_df)

Leave a Comment

Your email address will not be published.