I have two excel files that I want to join their tables together but the problem is there isn't a common column to join them with.
For example generalized to get to the point:
file 1
| customer_id |
|---|
| 1 |
| 2 |
file 2
| purchase_id | purchase_amount |
|---|---|
| 501 | five |
| 502 | two |
I want it to look like this
| customer_id | purchase_id | purchase_amount |
|---|---|---|
| 1 | 501 | five |
| 2 | 502 | two |
so far I have tried this but with no avail.
import pandas
f1= pandas.read_excel(file1.xlsx)
f2= pandas.read_excel(file2.xlsx)
#merge file
f3=f1[["customer_id"]].merge(f2[["purchase_id", "purchase_amount"]])
CodePudding user response:
You are looking for pd.concat:
import pandas as pd # alias pandas
df1 = pd.read_excel('file1.xlsx')
df2 = pd.read_excel('file2.xlsx')
out = pd.concat([df1, df2], axis=1)
print(out)
# Output
customer_id purchase_id purchase_amount
0 1 501 five
1 2 502 two
