I'm new to pandas. Given two data frames:
df_1
| product_id | product_price | invoice_total |
|---|---|---|
| p1 | 100 | 200 |
| p2 | 200 | 300 |
| p3 | 300 | 600 |
| p4 | 400 | 700 |
df_2
| product_id | quantity | invoice_total |
|---|---|---|
| p1 | 8 | 700 |
| p6 | 3 | 900 |
| p2 | 5 | 600 |
I want to check if the product id in df1 is similar to that of df2 and if so, pick the value of invoice total in df2.
I've tried the for loop:
df_new = pd.DataFrame()
for i in df1.product_id:
for j in df2.product_id:
if i == j:
# return the value of df2.invoice_total and append to the df_new.
But I believe there's a better way.
The result should be something like this:
| product_id | invoice_total |
|---|---|
| p1 | 700 |
| p2 | 600 |