pandas sub() method | subtract value from dataframe and column
In this pandas tutorial, we will discuss about:
-
pandas sub method,
-
pandas sub dataframe example,
-
subtract value from pandas column
Before going ahead with pandas sub function and subtract value from pandas column, lets learn a bit about 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 will create DataFrame with help of DataFrame() function. As it 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 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: Dataframe is created below
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 pandas sub function on this dataframe.
pandas sub method
We can subtract a value to the entire dataframe or to the particular column in the dataframe using sub() method or pandas sub method.
Syntax:
dataframe.sub(value)
(or)
dataframe['column'].sub(value)
where,
1. dataframe is the input dataframe
2. column refers column name where value to be subtracted
3. value represents the numeric value to be subtracted with dataframe column/ entire dataframe.
Lets see few exampes of using pandas sub function.
Example 1: pandas sub dataframe
In this pandas sub dataframe example, we will subtract some values from the entire 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'])
# subtract 4 from the dataframe
print(data.sub(4))
print()
# subtract 20 from the dataframe
print(data.sub(20))
print()
# subtract 0 from the dataframe
print(data.sub(0))
Output: pandas sub dataframe example result
length breadth area
one 1.6 8.9 16
two 3.8 0.5 52
three 0.5 17.5 39
four 1.3 2.0 41
length breadth area
one -14.4 -7.1 0
two -12.2 -15.5 36
three -15.5 1.5 23
four -14.7 -14.0 25
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
We can also use '-' operator to subtract values.
Example: pandas sub dataframe 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'])
# subtract 4 from the dataframe
print(data-4)
print()
# subtract 20 from the dataframe
print(data-20)
print()
# subtract 0 from the dataframe
print(data-0)
Output: pandas sub dataframe example result
length breadth area
one 1.6 8.9 16
two 3.8 0.5 52
three 0.5 17.5 39
four 1.3 2.0 41
length breadth area
one -14.4 -7.1 0
two -12.2 -15.5 36
three -15.5 1.5 23
four -14.7 -14.0 25
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
Lets see another way to subtract value from dataframe column.
Example 2: subtract value from pandas column
In this subtract value from dataframe column example, we will subtract some values to the particular column of the dataframe.
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'])
# subtract 4 from the length column
print(data['length'].sub(4))
print()
# subtract 20 from the breadth column
print(data['breadth'].sub(20))
Output: subtract value from pandas column example result
one 1.6
two 3.8
three 0.5
four 1.3
Name: length, dtype: float64
one -7.1
two -15.5
three 1.5
four -14.0
Name: breadth, dtype: float64
We can also use - operator to subtract values.
Example: subtract value from pandas column 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'])
# subtract 4 from the length column
print(data['length']-4)
print()
# subtract 20 from the breadth column
print(data['breadth']-20)
Output: subtract value from pandas column example result
one 1.6
two 3.8
three 0.5
four 1.3
Name: length, dtype: float64
one -7.1
two -15.5
three 1.5
four -14.0
Name: breadth, dtype: float64
This wraps up the session on subtract value from pandas column using pandas sub function.
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 16,2022