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

Time:01-12

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.

CodePudding user response:

I think you are looking for transform:

df['Sell_price'] = df.groupby(['Date', 'Product_id'])['Sell_price'].transform('max')
  • Related