Home > database >  Python Pandas: Change a value in pd1 based of pd2 with a different name
Python Pandas: Change a value in pd1 based of pd2 with a different name

Time:06-12

i have 2 data frames where the index is the same value but different name. I need to add a column to DF1 but get that information from DF2

DF1:
    SKU       X        Y         Z      
    1234      0        0         0
    5642      0        0         0

DF2:
    AH SKU     X        Y         Z      Total
    1234      4        5         1        10
    5642      1        0         1         2 

So I know I can add the total column to DF1 by doing

df1 ["Total"]

How can I now have that total from DF2 to DF1 making sure the SKU and AH SKU are matching?

CodePudding user response:

You would do it like this, using default index matching.

df1["Total"] = df2["Total"]

That the indexes have different names shouldn't matter in this case (there's only one index level).

Make sure that those "columns" you pointed to in the answer really are the index: print(df1.index) should have something named SKU, and so on.

  • Related