Python | Pandas dataframe.cummax()
Last Updated :
16 Nov, 2018
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas dataframe.cummax()
is used to find the cumulative maximum value over any axis. Each cell is populated with the maximum value seen so far.
Syntax: DataFrame.cummax(axis=None, skipna=True, *args, **kwargs)
Parameters:
axis : {index (0), columns (1)}
skipna : Exclude NA/null values. If an entire row/column is NA, the result will be NA
Returns: cummax : Series
Example #1: Use cummax()
function to find the cumulative maximum value along the index axis.
import pandas as pd
df = pd.DataFrame({ "A" :[ 5 , 3 , 6 , 4 ],
"B" :[ 11 , 2 , 4 , 3 ],
"C" :[ 4 , 3 , 8 , 5 ],
"D" :[ 5 , 4 , 2 , 8 ]})
df
|
Output :
Now find the cumulative maximum value over the index axis
Output :
Example #2: Use cummax()
function to find the cumulative maximum value along the column axis.
import pandas as pd
df = pd.DataFrame({ "A" :[ 5 , 3 , 6 , 4 ],
"B" :[ 11 , 2 , 4 , 3 ],
"C" :[ 4 , 3 , 8 , 5 ],
"D" :[ 5 , 4 , 2 , 8 ]})
df.cummax(axis = 1 )
|
Output :
Example #3: Use cummax()
function to find the cumulative maximum value along the index axis in a data frame with NaN
value.
import pandas as pd
df = pd.DataFrame({ "A" :[ 5 , 3 , None , 4 ],
"B" :[ None , 2 , 4 , 3 ],
"C" :[ 4 , 3 , 8 , 5 ],
"D" :[ 5 , 4 , 2 , None ]})
df.cummax(axis = 0 , skipna = True )
|
Output :
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...