add() function in pandas | how to add value in pandas dataframe with examples?
In this pandas tutorial, we will discuss about:
-
add function in pandas,
-
add function in pandas example
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 able to create this DataFrame using DataFrame() method. But this is available in pandas module, so we have to import pandas module.
Syntax to create dataframe:
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: 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
Lets use this dataframe to understand add in pandas dataframe. We will see few examples on how to add value in pandas dataframe?
add function in pandas
We can add a value to the entire daatframe or to the particular column in the dataframe using add() method or add function in pandas.
Syntax:
dataframe.add(value)
(or)
dataframe['column'].add(value)
where,
1. dataframe is the input dataframe
2. column refers column name where value to be added
3. value represents the numeric value to be added with dataframe column/ entire dataframe.
Lets now see how we can apply add in pandas dataframe.
Example 1: how to add value in pandas dataframe?
In this add function in pandas example, we will add some values to 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'])
# add 4 to the dataframe
print(data.add(4))
print()
# add 20 to the dataframe
print(data.add(20))
print()
# add 0 to the dataframe
print(data.add(0))
Output: add function in pandas result
length breadth area
one 9.6 16.9 24
two 11.8 8.5 60
three 8.5 25.5 47
four 9.3 10.0 49
length breadth area
one 25.6 32.9 40
two 27.8 24.5 76
three 24.5 41.5 63
four 25.3 26.0 65
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 add values.
Example: how to add value in pandas 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'])
# add 4 to the dataframe
print(data+4)
print()
# add 20 to the dataframe
print(data+20)
print()
# add 0 to the dataframe
print(data+0)
Output: add function in pandas result
length breadth area
one 9.6 16.9 24
two 11.8 8.5 60
three 8.5 25.5 47
four 9.3 10.0 49
length breadth area
one 25.6 32.9 40
two 27.8 24.5 76
three 24.5 41.5 63
four 25.3 26.0 65
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
Example 2: how to add value in pandas dataframe?
In this add function in pandas example, we will add 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'])
# add 4 to the length column
print(data['length'].add(4))
print()
# add 20 to the breadth column
print(data['breadth'].add(20))
Output: add function in pandas result
one 9.6
two 11.8
three 8.5
four 9.3
Name: length, dtype: float64
one 32.9
two 24.5
three 41.5
four 26.0
Name: breadth, dtype: float64
We can also use + operator to add values.
Example: how to add value in pandas 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'])
# add 4 to the length column
print(data['length']+4)
print()
# add 20 to the breadth column
print(data['breadth']+20)
Output: add in pandas dataframe result
one 9.6
two 11.8
three 8.5
four 9.3
Name: length, dtype: float64
one 32.9
two 24.5
three 41.5
four 26.0
Name: breadth, dtype: float64
This wraps up our session on how to add value in pandas dataframe using add function in 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 14,2022