简体   繁体   中英

find the maximum value for each streak of numbers in another column in pandas

I have a dataframe like this :

 df = pd.DataFrame({'dir': [1,1,1,1,0,0,1,1,1,0], 'price':np.random.randint(100,200,10)})

      dir  price 
    0    1    100
    1    1    150
    2    1    190
    3    1    194
    4    0    152
    5    0    151
    6    1    131
    7    1    168
    8    1    112
    9    0    193

and I want a new column that shows the maximum price as long as the dir is 1 and reset if dir is 0. My desired outcome looks like this:

   dir  price   max
0    1    100   194
1    1    150   194
2    1    190   194
3    1    194   194
4    0    152   NaN
5    0    151   NaN
6    1    131   168
7    1    168   168
8    1    112   168
9    0    193   NaN

Use transform with max for filtered rows:

#get unique groups for consecutive values
g = df['dir'].ne(df['dir'].shift()).cumsum()
#filter only 1
m = df['dir'] == 1
df['max'] = df[m].groupby(g)['price'].transform('max')
print (df)
   dir  price    max
0    1    100  194.0
1    1    150  194.0
2    1    190  194.0
3    1    194  194.0
4    0    152    NaN
5    0    151    NaN
6    1    131  168.0
7    1    168  168.0
8    1    112  168.0
9    0    193    NaN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM