Python | Pandas series.cummax() to find Cumulative maximum of a series
Last Updated :
20 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
Series.cummax()
is used to find Cumulative maximum of a series. In cumulative maximum, the length of returned series is same as input series and every element is equal to the greater one between current element and previous element.
Syntax: Series.cummax(axis=None, skipna=True)
Parameters:
axis: 0 or ‘index’ for row wise operation and 1 or ‘columns’ for column wise operation.
skipna: Skips NaN addition for elements after the very next one if True.
Return type: Series
Example #1:
In this example, a series is created from a Python list. The list also contains a Null value and the
skipna
parameter is kept default, that is True.
Python3 1==
# importing pandas module
import pandas as pd
# importing numpy module
import numpy as np
# making list of values
values = [3, 4, np.nan, 7, 2, 0]
# making series from list
series = pd.Series(values)
# calling method
cummax = series.cummax()
# display
cummax
Output:
0 3.0
1 4.0
2 NaN
3 7.0
4 7.0
5 7.0
dtype: float64
Explanation: Cummax is comparison of current value with previous value. The first element is always equal to first of caller series.
3
4 (4>3)
NaN (Since NaN cannot be compared to integer values)
7 (7>4)
7 (7>2)
7 (7>0)
Example #2: Keeping
skipna = False
In this example, a series is created just like in the above example. But the skipna parameter is kept False. Hence NULL values won’t be ignored and it would be compared every time on it’s occurrence.
Python3 1==
# importing pandas module
import pandas as pd
# importing numpy module
import numpy as np
# making list of values
values = [9, 4, 33, np.nan, 0, 1, 76, 5]
# making series from list
series = pd.Series(values)
# calling method
cummax = series.cummax(skipna = False)
# display
cummax
Output:
0 9.0
1 9.0
2 33.0
3 NaN
4 NaN
5 NaN
6 NaN
7 NaN
dtype: float64
Explanation: Just like in the above example, maximum of current and previous values were stored at every position until NaN occurred. Since NaN compared with anything returns NaN and skipna parameter is kept False, the cumulative maximum after its occurrence is NaN due to comparison of all the values with NaN.
Similar Reads
Python | Pandas Series.cummin() to find cumulative minimum of a series 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 Series.cummin() is used to find Cumulative minimum of a series. In cumulative m
2 min read
Python | Pandas Series.cumsum() to find cumulative sum of a Series Pandas Series.cumsum() is used to find Cumulative sum of a series. In cumulative sum, the length of returned series is same as input and every element is equal to sum of all previous elements. Syntax: Series.cumsum(axis=None, skipna=True) Parameters: axis: 0 or 'index' for row wise operation and 1 o
2 min read
Python | Pandas series.cumprod() to find Cumulative product of a Series 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 Series.cumprod() is used to find Cumulative product of a series. In cumulative
3 min read
Cumulative sum of a column in Pandas - Python Cumulative sum of a column in Pandas can be easily calculated with the use of a pre-defined function cumsum(). Syntax: cumsum(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 NARetur
2 min read
Cumulative percentage of a column in Pandas - Python Cumulative Percentage is calculated by the mathematical formula of dividing the cumulative sum of the column by the mathematical sum of all the values and then multiplying the result by 100. This is also applicable in Pandas Data frames.Here, the pre-defined cumsum() and sum() functions are used to
1 min read
Find the sum and maximum value of the two column in excel file using Pandas In these articles, we will discuss how to read data from excel and perform some mathematical operation and store it into a new column in DataFrame. Suppose our excel file looks like this. sample_data.xlsx Then we have to compute the sum of two-column and find out the maximum value and store into a n
2 min read