How to Merge 2 CSV Files Together by Multiple Columns in Python

How to merge 2 CSV files together by multiple columns in Python

I believe you need to use ['Date', 'Ticker'] instead of just 'Date'. Also you might need to specify the how argument depending on what you want.

Use pandas to combine 2 CSV files

IIUC you want to be able to tell you function which column is to be handled as Z in df2 and then concat both lists:

def concat_df_on_z(df1, df2, z_col):
df2 = df2[['X', 'Y', z_col]].rename(columns={z_col: 'Z'})
return pd.concat([df1, df2])

df3 = concat_df_on_z(df1, df2, 'B')
print(df3)

Output:

          X         Y      Z
0 626066.4 234058.2 6.69
1 626066.4 234059.2 6.89
2 626066.4 234060.2 7.06
0 627839.0 232463.4 14.46
1 627839.0 232463.1 14.46


Related Topics



Leave a reply



Submit