Skip to main content
panda

There are numerous instances while dealing with data science or machine learning tasks when we have to perform very basic mathematical operations. Pandas help in data handling and manipulation to a large extent, thus it is quite obvious that Pandas have functions for mathematical operations. So in this tutorial we will learn more about these pandas mathematical functions namely add(), sub(), mul(), div(), sum() and agg(). We will learn more about these pandas mathematical functions by looking at their syntax and examples.

Pandas Addition : add()

The pandas addition function performs the addition of dataframes. The addition is performed element-wise.

pandas.DataFrame.add(other, axis=’columns’, level=None, fill_value=None)

other: scalar, sequence, Series, or DataFrame – This parameter consists of any single or multiple element data structure or list-like object.

axis : {0 or ‘index’, 1 or ‘columns’} – This is used for deciding the axis on which the operation is applied.

level : int or label – The level parameter is used for broadcasting across a level and matching Index values on the passed MultiIndex level.

fill_value : float or None, default None – Whenever the dataframes have missing values, then to fill existing missing (NaN) values, we can use fill_value parameter.

Pandas Subtract : sub()

The subtract function of pandas is used to perform subtract operation on dataframes.

pandas.DataFrame.sub(other, axis=’columns’, level=None, fill_value=None)

other : scalar, sequence, Series, or DataFrame – This parameter consists any single or multiple element data structure, or list-like object.

axis : {0 or ‘index’, 1 or ‘columns’} – This is used for deciding the axis on which the operation is applied.

level : int or label – The level parameter is used for broadcasting across a level and matching Index values on the passed MultiIndex level.

fill_value : float or None, default None – Whenever the dataframes have missing values, then to fill existing missing (NaN) values, we can use fill_value parameter.

Pandas Multiply : mul()

The multiplication function of pandas is used to perform multiplication operations on dataframes.

pandas.DataFrame.mul(other, axis=’columns’, level=None, fill_value=None)

other : scalar, sequence, Series, or DataFrame – This parameter consists any single or multiple element data structure, or list-like object.

axis : {0 or ‘index’, 1 or ‘columns’} – This is used for deciding the axis on which the operation is applied.

level : int or label – The level parameter is used for broadcasting across a level and matching Index values on the passed MultiIndex level.

fill_value : float or None, default None – Whenever the dataframes have missing values, then to fill existing missing (NaN) values, we can use fill_value parameter.

Pandas Division : div()

The division function of pandas is used to perform division operation on dataframes.

pandas.DataFrame.div(other, axis=’columns’, level=None, fill_value=None)

other : scalar, sequence, Series, or DataFrame – This parameter consists any single or multiple element data structure, or list-like object.

axis : {0 or ‘index’, 1 or ‘columns’} – This is used for deciding the axis on which the operation is applied.

level : int or label – The level parameter is used for broadcasting across a level and matching Index values on the passed MultiIndex level.

fill_value : float or None, default None – Whenever the dataframes have missing values, then to fill existing missing (NaN) values, we can use fill_value parameter.

Pandas Sum : sum()

The sum function helps in finding the sum of the values for desired axis.

pandas.DataFrame.sum(axis=None, skipna=None, level=None, numeric_only=None, min_count=0, kwargs)

axis : {index (0), columns (1)} – This is the axis where the function is applied.

skipna : bool, default True – It is used to decide whether the NA/Null values should be dropped/skipped or not while computation.

level : int or level name, default None – It used for deciding the level, generally in case of multindex dataframes.

numeric_only : bool,default None – It used to decide whether to include only float, int, boolean columns. If None, will attempt to use everything

min_count : int,default 0 – The required number of valid values to perform the operation.

kwargs : Additional Arguments.

Pandas Aggregate: agg()

The pandas aggregate function is used to aggregate using one or more operations over desired axis.

pandas.dataframe.agg(func, axis=0, *args, kwargs)

func : function, str, list or dict – This is the function used for aggregating the data.

axis : {0 or ‘index’, 1 or ‘columns’}, default 0 – The axis over which the operation is applied.

args : These are the positional arguments to pass to func.

kwargs : Additional keyword arguments.

Conclusion

Reaching to the end of this article, we learned about various mathematical operations like add(), sub(), mul(), div(), sum() and agg(). These basic mathematical operations can be performed easily with the help of pandas library. Since we deal with mathematical tasks in our data science interactions, these pandas operations will prove to very handy.

Tags
Submitted by shiksha.dahiya on February 13, 2021

Shiksha is working as a Data Scientist at iVagus. She has expertise in Data Science and Machine Learning.

About

Elix is a premium wordpress theme for portfolio, freelancer, design agencies and a wide range of other design institutions.