How can I apply the results of groupby command to all rows?

Question:

I have a dataframe as follows:

enter image description here

I want to have the max of the ‘Sell_price’ column according to ‘Date’ and ‘Product_id’ columns, without losting the dimension of dataframe as:

enter image description here

Since my data is very big, without a doubt using ‘for’ command is not logical.

Answers:

I think you are looking for transform:

df['Sell_price'] = df.groupby(['Date', 'Product_id'])['Sell_price'].transform('max')
Answered By: SomeDude
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.