Pandas math Operations on columns || Mathematical function on DataFrame

Pandas math operations on column

Mathematical function on DataFrame

 
Pandas math Operations on columns

There are a number of mathematical functions.

  1. Pandas Addition : add() function

The pandas add() function performs the addition of two DataFrames. 

Syntax

pandas.DataFrame.add(other, axis=’columns’, level=None, fill_values=None)
Where:-
        other         :-   scalar, sequence, Series, or any DataFrame
        axis           :-   ( 0 or index,   1 or column )
        level          :-  int to label 
        fill_value  :-  float or None, by default None
 
Example 1:- Addition of DataFrame using add() function
 

import pandas as pd
df=pd.DataFrame( { ‘speed’ : [110,90,100], ‘weight’ : [500,550,600] }, index=[‘Audi’, ‘Suzuki’,’BMW’ ] )
print(“before add() function “)
print(df)
df.add(50)
print(“After add() function “)
print(df)

 
Its output will be-
 
before add() function

 

                speed     weight
Audi         110        500
Suzuki       90         550
BMW       100        600

 

 
After add() function
                speed     weight
Audi         160        550
Suzuki      140        600
BMW       150        650
 
Note:- You can see that the scaler value in add() function will add them to all elements in DataFrame.
 
Example 2:- In this example, we will see the addition of two DataFrame for this we will pass a DataFrame in add() function as an argument.
 

import pandas as pd
df1=pd.DataFrame( { ‘speed’ : [110,90,100], ‘weight’ : [500,550,600] }, index=[‘Audi’, ‘Suzuki’,’BMW’ ] )
print(“before add() function “)
print(df1)
df2=add(df1)
print(“After add() function “)
print(df2)

 
Its output will be-
 
before add() function
                speed     weight
Audi         110        500
Suzuki       90         550
BMW       100        600
 
After add() function
                speed     weight
Audi         220        1000
Suzuki      180        1100
BMW       200        1200
 

    2. Pandas Subtraction : sub() function

 
Example 1:- 

import pandas as pd
df=pd.DataFrame( { ‘speed’ : [110,90,100], ‘weight’ : [500,550,600] }, index=[‘Audi’, ‘Suzuki’,’BMW’ ] )
print(“before sub() function “)
print(df)
df=sub( [ 25,50], axis=’column’)
print(“After sub() function “)
print(df)

 
 
 
Its output will be-
 
before sub() function
                speed     weight
Audi         110        500
Suzuki       90         550
BMW       100        600
 
After sub() function
                speed     weight
Audi         85        450
Suzuki      65        500
BMW       75        550
 
Note:- we can see separate values is subtracted from each column of DataFrame.
 
 Example 2:-
 

import pandas as pd
df=pd.DataFrame( { ‘speed’ : [110,90,100], ‘weight’ : [500,550,600] }, index=[‘Audi’, ‘Suzuki’,’BMW’ ] )
print(“before sub() function “)
print(df)
df=sub (pd.Series( [10,20,30], index=[‘Audi’, ‘Suzuki’,’BMW’ ]), axis =’index’)
print(“After sub() function “)
print(df)

 
 
 
Its output will be-
 
before sub() function
                speed     weight
Audi         110        500
Suzuki       90         550
BMW       100        600
 
After sub() function
                speed     weight
Audi         100        490
Suzuki      70          530
BMW       70          570
 

3. Pandas Multipy : mul() function

The mul() function is used to perform multiplication operation on DataFRame.
 
Example 1 :- 
 
import pandas as pd df1=pd.DataFrame( { ‘speed’ : [100,110,80], ‘weight’ : [500,550,600] }, index=[‘Audi’, ‘Suzuki’,’BMW’ ] )
df2=pd.DataFrame( { ‘speed’ : [80,90,100], ‘weight’ : [500,550,600] }, index=[‘Audi’, ‘Suzuki’,’BMW’ ] )
print(“before mul() function “)
print(df1)
print(df2)
result=df1.mul(df2)
print(“After mul() function “)
print(result)
 
Its output will be-
 
before mul() function
                speed     weight
Audi         100        500
Suzuki      100        550
BMW       80          600
 
                speed     weight
Audi         80        450
Suzuki      90        500
BMW       100       550
 
After mul() function 
                speed        weight
Audi         8000        225000
Suzuki      9000        275000
BMW       8000        330000
 

4. Pandas Divion : div() function

The div() function is used to perform division operation on DataFRame.    
Example:- 
    

import pandas as pd
df=pd.DataFrame( { ‘speed’ : [110,90,100], ‘weight’ : [500,550,600] }, index=[‘Audi’, ‘Suzuki’,’BMW’ ] )
print(“before add() function “)
print(df)
df.div(10)
print(“After div() function “)
print(df)

 
 

 

Its output will be-

 
before div() function
                speed     weight
Audi         110        500
Suzuki       90         550
BMW       100        600
 
After div() function
                speed     weight
Audi         11.0        50.0
Suzuki      9.0          55.0
BMW       10.0        60.0
In this post we have studied pandas arithmetic operations on columns.

If you like my post please share it with your friends by simply clicking the below social media button, and Don’t forget to comment so we will update ourselves.

Share in a Single Click

Leave a Reply

Your email address will not be published. Required fields are marked *