Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform dataframe based on three first rows in pandas

I have a data frame like this (but much bigger) and I am trying to use transform to get the max based ONLY on the first 3 rows of each group.

     df10 = pd.DataFrame({
      'Price': [1,2,3,4,5,10,20,30,40,50],
      'Stock': ['AAPL', 'AAPL', 'AAPL', 'AAPL', 'AAPL', 'IBM','IBM','IBM','IBM','IBM']
     })

This syntax works for the entire column

df10['max_top_3']=df10.groupby("Stock").Price.transform('max')

But I want the 'max_top_3' column to show 3 and 30 respectively for AAPL and IBM >> which is the max number of the first 3 entries in that column

I tried something like this and it gave an error

df10['max_top_3']=df10.groupby("Stock").Price.head(3).transform('max')
like image 518
Michael Avatar asked Mar 03 '26 15:03

Michael


1 Answers

You can chain the head in transform with a lambda:

df10.groupby("Stock").Price.transform(lambda x: x.head(3).max())

0     3
1     3
2     3
3     3
4     3
5    30
6    30
7    30
8    30
9    30
Name: Price, dtype: int64
like image 98
anky Avatar answered Mar 06 '26 19:03

anky



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!