'merging price table onto multiple columns in primary df

I have a price table:

product number price
10 50
20 70

That i want to merge on my "main dataframe" with end result looking like this:

productnr1 productnr2 productnr3 price1 price2 price3

So basically for each product number column it is supposed to look up the price in the price dataframe.

I have tried the following code, which works most of the time, but afterwards i did find some weird errors:

for i in range(1,15):
        main_df[f'price_{i}'] = main_df.merge(prices_df,how="left",left_on=f"productnr_{i}",right_on="produktnr")["price"]

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source