max() of previous values

28 views
Skip to first unread message

Juan Na

unread,
Jun 15, 2016, 4:20:45 PM6/15/16
to pyd...@googlegroups.com
Hi,
  I want to create a column in a dataframe with value of max()  of previous values of other column.

For example, I have this dataframe:

Index  Value
0        5
1        6
2        8
3        3
4        4
5        13
...
and I want a new column like

Index  Value    MxValue
0        6           6
1        5           6
2        8           8
3        3           8
4        4           8
5        13         13
...

I know this way:

for n in df.index:
         dfx.loc[n,'MxValue'] = max  ( df.ix[n,'Value'] , df.ix [:n-1 , 'Value' ].max() )

but is very slow for big dataframes. Anyone know another, an more fast, way?

Thanks in advance

Stephan Hoyer

unread,
Jun 15, 2016, 4:25:24 PM6/15/16
to pyd...@googlegroups.com
I believe the "cummax" method does exactly what you're looking for:

--
You received this message because you are subscribed to the Google Groups "PyData" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pydata+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Juan Na

unread,
Jun 16, 2016, 2:03:14 PM6/16/16
to pyd...@googlegroups.com
Thanks Stephan, is exactly I'm looking for. I suspected it was a very useful thing but do not fit.


Reply all
Reply to author
Forward
0 new messages