Home > Mobile >  Add data into a dataframe based on whether ID exists
Add data into a dataframe based on whether ID exists

Time:04-28

I have two dataframes, data and import_df (see below). what I would like to do is add anther column to data and populate it with the "mass" column from import_df if the Item ID matches. I would like to name this new column a specific string which is known/generated.

data

ItemID Name
1276   Part 1
1382   Part 2
.      .
.      .
.      .

import_df

ItemID Name
1276   15
1672   32
1662   90
1382   32
.      .
.      .
.      .

End goal would be: data

ItemID Name    date variable (mass)
1276   Part 1  15
1382   Part 2  32
.      .
.      .
.      .

I think I could manage using a for loop that cycles through each row of both data frames but I think there must be a better way.

CodePudding user response:

So basicaly you need to join two dataframes on a common column. Do this:

data = data.merge(import_df, on="ItemID")
  • Related