pandas using .where() to replace in .groupby() object

jorijnsmit :

Consider a dataframe which contains several groups of integers:

d = pd.DataFrame({'label': ['a','a','a','a','b','b','b','b'], 'value': [1,2,3,2,7,1,8,9]})
d
    label   value
0   a   1
1   a   2
2   a   3
3   a   2
4   b   7
5   b   1
6   b   8
7   b   9

For each of these groups of integers, each integer has to be bigger or equal to the previous one. If not the case, it takes on the value of the previous integer. I replace using

s.where(~(s < s.shift()), s.shift())

which works fine for a single series. I can even group the dataframe, and loop through each extracted series:

grouped = s.groupby('label')['value']
for _, s in grouped:
    print(s.where(~(s < s.shift()), s.shift()))
0    1.0
1    2.0
2    3.0
3    3.0
Name: value, dtype: float64
4    7.0
5    7.0
6    8.0
7    9.0
Name: value, dtype: float64

However, how do I now get these values back into my original dataframe?

Or, is there a better way to do this? I don't care for using .groupby and don't consider the for loop a pretty solution either...

Ben.T :

IIUC, you can use cummax in the groupby like:

d['val_max'] = d.groupby('label')['value'].cummax()
print (d)
  label  value  val_max
0     a      1        1
1     a      2        2
2     a      3        3
3     a      2        3
4     b      7        7
5     b      1        7
6     b      8        8
7     b      9        9

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360436&siteId=1