0

I have two data frames. df1 has this structure ['Key', 'Name']. df2 has this structure ['Key', 'Price'].

Both have different numbers of rows. Now, I want to check if 'Key' from df1 exists in 'Key' column of df2. If yes, then copy the corresponding value of 'Price' from df2 and save it in data frame df3 as a new column called 'lookup'. If no then save -1. Note that df3 is a copy of df1.

  • 1
    use `merge` and pass the `validate` parameter. See the docs here: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html As an example, try something like `df3=df1.merge(df2, how='left', on='Key', indicator='one_to_many')`. From there you can manipulate the data more specifically based off that output. – David Erickson Apr 01 '21 at 11:52
  • Also, please provide a minimum reproducible example ( https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples ) of sample input and output. The problem is unclear and you might also be able to use `isin`: – David Erickson Apr 01 '21 at 11:55

0 Answers0