Articles

cummin() method in pandas | cummin method example

cummin() method in pandas | cummin method example


In this pandas tutorial, we will discuss about: 

  • cummin method in pandas,
  • cummin method in pandas example

Before moving ahead with example of cummin method in pandas, lets create pandas dataframe.

 

DataFrame in pandas is an two dimensional data structure that will store data in two dimensional format. One dimension refers to a row and second dimension refers to a column, So It will store the data in rows and columns.

We can create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.

Syntax:

pandas.DataFrame(data)

Where, data is the input dataframe. The data can be a dictionary that stores list of values with specified key.

 

Example: Create pandas dataframe

In this example, we will create a dataframe with 4 rows and 3 columns with building data and assign indices through index parameter.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

#display the dataframe
print(data)

Output: Pandas dataframe is created

       length  breadth  area
one       5.6     12.9    20
two       7.8      4.5    56
three     4.5     21.5    43
four      5.3      6.0    45

Now lets use this dataframe to understand cummin method in pandas.


cummin() method in pandas

cummin() method in pandas will return the cumulative minimum of values for the given dataframe.

Syntax:

dataframe.cummin(axis,skipna)

This will return the entire dataframe

Parameters:

  1. axis=0 specifies row and axis= 1 specifies column to get cumulative maximum along row/column
  2. skipna will take boolean values - True or False. If False,It will consider NaN values and If True,It will not consider NaN values in cumulative minimum operation.

If we want to get the  cumulative minimum of values in a column for the given dataframe, we have to specify the column.

Syntax:

dataframe['column'].cummin()

where, dataframe is the input dataframe and column is the column name.

 

This will return the specified column cumulative minimum in the given dataframe.

Example 1cummin method in pandas example

Here in cummin method in pandas example, we will get the cumulative minimum for the entire dataframe and in a specific column.

import pandas as pd

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[5.6,7.8,4.5,5.3],

                   "breadth":[12.9,4.5,21.5,6.0],

                    "area":[20,56,43,45]

                   },index=['one','two','three','four'])

# get the cumulative minimum 
print(data.cummin())

print()

# get the cumulative minimum from length column
print(data['length'].cummin())

print()

# get the cumulative minimum from area column
print(data['area'].cummin())

Outputcummin method in pandas example result

       length  breadth  area
one       5.6     12.9    20
two       5.6      4.5    20
three     4.5      4.5    20
four      4.5      4.5    20

one      5.6
two      5.6
three    4.5
four     4.5
Name: length, dtype: float64

one      20
two      20
three    20
four     20
Name: area, dtype: int64

Example 2: cummin method in pandas example

In this cummin method in pandas example, we will be dealing with skipna parameter.

import pandas as pd
import numpy as np

#create dataframe from the college data
data= pd.DataFrame({
                    'length':[np.nan,7.8,4.5,np.nan],

                   "breadth":[12.9,4.5,21.5,np.nan],

                    "area":[2,np.nan,56,43]

                   },index=['one','two','three','four'])

# get the cumulative minimum with out nan values
print(data.cummin(skipna=True))

print()

# get the cumulative minimum  by considering nan values
print(data.cummin(skipna=False))

Outputcummin method in pandas example result

       length  breadth  area
one       NaN     12.9   2.0
two       7.8      4.5   NaN
three     4.5      4.5   2.0
four      NaN      NaN   2.0

       length  breadth  area
one       NaN     12.9   2.0
two       NaN      4.5   NaN
three     NaN      4.5   NaN
four      NaN      NaN   NaN

Thus we have seen two example of cummin method in pandas.


Pandas

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1400+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 11,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!