Articles

div function in pandas | divide a dataframe & column in pandas examples

div function in pandas | divide a dataframe & column in pandas examples


In this pandas tutorial, we will discuss about:

  • div function in pandas,
  • divide a dataframe in pandas,
  • divide a column by a number in pandas

Before going ahead with learning div function in pandas, lets have a look at pandas dataframe.

 

Pandas DataFrame 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 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 building 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: 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 div in pandas.


div function in pandas

We can divide entire dataframe or the particular column with a value in the dataframe using div() method or div function in pandas.

Syntax div in pandas:

dataframe.div(value)
(or)
dataframe['column'].div(value)

where,

1. dataframe is the input dataframe

2. column refers column name where value to be divided.

3. value represents the numeric value to be divide with dataframe column/ entire dataframe.

 

Lets see few examples on div in pandas.


Example 1divide a dataframe in pandas

In this divide a dataframe in pandas example, we will divide entire dataframe with some values.

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'])

# divide the dataframe by 4
print(data.div(4))

print()

# divide the dataframe by 20
print(data.div(20))

print()

# divide the dataframe with 0
print(data.div(0))

Outputdivide a dataframe in pandas with 4, 20 and 0 result

       length  breadth   area
one     1.400    3.225   5.00
two     1.950    1.125  14.00
three   1.125    5.375  10.75
four    1.325    1.500  11.25

       length  breadth  area
one     0.280    0.645  1.00
two     0.390    0.225  2.80
three   0.225    1.075  2.15
four    0.265    0.300  2.25

       length  breadth  area
one       inf      inf   inf
two       inf      inf   inf
three     inf      inf   inf
four      inf      inf   inf

We can also use '/' operator to divide value to the dataframe.

Exampledivide a dataframe in pandas using / operator.

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'])

# divide dataframe by 4
print(data/4)

print()

# divide dataframe by 20
print(data/20)

print()

# divide dataframe by 0
print(data/0)

Output: Result for divide a dataframe in pandas with values 4, 20 and 0

       length  breadth   area
one     1.400    3.225   5.00
two     1.950    1.125  14.00
three   1.125    5.375  10.75
four    1.325    1.500  11.25

       length  breadth  area
one     0.280    0.645  1.00
two     0.390    0.225  2.80
three   0.225    1.075  2.15
four    0.265    0.300  2.25

       length  breadth  area
one       inf      inf   inf
two       inf      inf   inf
three     inf      inf   inf
four      inf      inf   inf

Now lets learn about divide column in pandas.


Example 2divide a column by a number in pandas

In this divide a column by a number in pandas example, we will divide with some values to the particular column of the dataframe.

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'])

# divide length column by 4
print(data['length'].div(4))

print()

# divide length column by 20
print(data['breadth'].div(20))

Outputdivide column in pandas result

one      1.400
two      1.950
three    1.125
four     1.325
Name: length, dtype: float64

one      0.645
two      0.225
three    1.075
four     0.300
Name: breadth, dtype: float64

We can also use '/'  operator to divide values.

Exampledivide a column by a number in pandas using /

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'])

# divide  breadth column by 4
print(data['length']/4)

print()

# divide  breadth column by 20
print(data['breadth']/20)

Outputdivide column in pandas result

one      1.400
two      1.950
three    1.125
four     1.325
Name: length, dtype: float64

one      0.645
two      0.225
three    1.075
four     0.300
Name: breadth, dtype: float64

Thus we have learned about divide a column by a number in pandas and divide a dataframe in pandas using div function 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 800+ Technical Articles on Python, R, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 19,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!