Pandas – Set value in column by looking up in another dataframe

  dataframe, pandas, python

I have two dataframes that have the following structure.

Dataframe A:

id date price
1 2021-09-01 null
1 2021-09-02 null
2 2021-09-01 null
2 2021-09-02 null
3 2021-09-01 null
3 2021-09-02 null

Dataframe B:

id price
1 100
2 200
3 300

I need to set the price in dataframe A, for each id, to the same value as the id has in dataframe B, regardless of the date in dataframe A.

So expected result is the following:

id date price
1 2021-09-01 100
1 2021-09-02 100
2 2021-09-01 200
2 2021-09-02 200
3 2021-09-01 300
3 2021-09-02 300

The data set is very large so need something efficient.

Happy to hear your suggestions 🙂

Source: Python Questions

LEAVE A COMMENT