+2 votes
in Programming Languages by (59.4k points)
I want to find the cumulative maximum along a given axis of a pandas dataframe. Which function should I use for it?

1 Answer

+2 votes
by (75.0k points)
selected by
 
Best answer

You can use the cummax() of the Pandas. It returns a cumulative maximum over a DataFrame or Series axis.

Here is an example:

>>> import pandas as pd
>>> df = pd.DataFrame({'A':[1,4,3,2,5], 'B':[10,23,11,12,25]})
>>> df.cummax(axis=1)
   A   B
0  1  10
1  4  23
2  3  11
3  2  12
4  5  25
>>> df.cummax(axis=0)
   A   B
0  1  10
1  4  23
2  4  23
3  4  23
4  5  25
>>>


...