0

I want to create a new column in a Pandas Dataframe which includes the maximum value of another column up to this Point in time.
For example:

time  | value | max value   
 1        2        2  
 2        4        4  
 3        3        4  
 4        3        4 
 5        6        6

Does someone have an idea how to create a Code for the "max value" column?
Thanks

j. DOE
  • 238
  • 1
  • 2
  • 15
  • I think I have to clarify my question. The first column is the index of the Dataframe and it is a timestamp. The column "max value" should include the maximum value of the column "value" up to the specific date in the row. – j. DOE Aug 09 '18 at 20:11
  • j. DOE care to write a more roboust sample of data that ensures we meet your requirements? – Scott Boston Aug 09 '18 at 20:19

2 Answers2

5

Use cummax:

df['max value'] = df['value'].cummax()

Output:

   time  value  max value
0     1      2          2
1     2      4          4
2     3      3          4
3     4      3          4
4     5      6          6
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
1

What you said is the definition of cummax.

Just out of curiosity, you can also use expanding, which might come to be useful if you want to apply a custom function it.

df.value.expanding().max() # or .apply(custom_function)

0    2.0
1    4.0
2    4.0
3    4.0
4    6.0
rafaelc
  • 57,686
  • 15
  • 58
  • 82