Pandas DataFrame - add() function
The Pandas add() function returns addition of dataframe and other, element-wise. It is equivalent to dataframe + other, but with support to substitute a fill_value for missing data as one of the parameters.
Syntax
DataFrame.add(other, axis='columns', level=None, fill_value=None)
Parameters
other |
Required. Specify any single or multiple element data structure, or list-like object. |
axis |
Optional. Specify whether to compare by the index (0 or 'index') or columns (1 or 'columns'). For Series input, axis to match Series index on. Default is 'columns'. |
level |
Optional. Specify int or label to broadcast across a level, matching Index values on the passed MultiIndex level. Default is None. |
fill_value |
Optional. Specify value to fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment. If data in both corresponding DataFrame locations is missing the result will be missing. Default is None. |
Return Value
Returns the result of the arithmetic operation.
Example: using add() on whole DataFrame
In the example below, a DataFrame df is created. The add() function is used to add a scalar value to the whole DataFrame.
import pandas as pd import numpy as np df = pd.DataFrame({ "Bonus": [5, 3, 2, 4], "Salary": [60, 62, 65, 59]}, index= ["John", "Marry", "Sam", "Jo"] ) print("The DataFrame is:") print(df) #adding 3 to all entries of the DataFrame print("\ndf.add(3) returns:") print(df.add(3))
The output of the above code will be:
The DataFrame is: Bonus Salary John 5 60 Marry 3 62 Sam 2 65 Jo 4 59 df.add(3) returns: Bonus Salary John 8 63 Marry 6 65 Sam 5 68 Jo 7 62
Example: Adding different value to different column
Different scalar value can be added to different column by providing other argument as a list. Consider the following example:
import pandas as pd import numpy as np df = pd.DataFrame({ "Bonus": [5, 3, 2, 4], "Salary": [60, 62, 65, 59]}, index= ["John", "Marry", "Sam", "Jo"] ) print("The DataFrame is:") print(df) #adding 5 to all entries of Bonus column #adding 10 to all entries of Salary column print("\ndf.add([5,10]) returns:") print(df.add([5,10]))
The output of the above code will be:
The DataFrame is: Bonus Salary John 5 60 Marry 3 62 Sam 2 65 Jo 4 59 df.add([5,10]) returns: Bonus Salary John 10 70 Marry 8 72 Sam 7 75 Jo 9 69
Example: using add() on selected columns
Instead of whole DataFrame, the add() function can be applied on selected columns. Consider the following example.
import pandas as pd import numpy as np df = pd.DataFrame({ "Bonus": [5, 3, 2, 4], "Last Salary": [58, 60, 63, 57], "Salary": [60, 62, 65, 59]}, index= ["John", "Marry", "Sam", "Jo"] ) print("The DataFrame is:") print(df) #adding 3 to all entries of Salary column print("\ndf['Salary'].add(3) returns:") print(df["Salary"].add(3)) #adding 3 to all entries of Salary column #adding 2 to all entries of Bonus column print("\ndf[['Salary','Bonus']].add([3,2]) returns:") print(df[["Salary", "Bonus"]].add([3,2]))
The output of the above code will be:
The DataFrame is: Bonus Last Salary Salary John 5 58 60 Marry 3 60 62 Sam 2 63 65 Jo 4 57 59 df['Salary'].add(3) returns: John 63 Marry 65 Sam 68 Jo 62 Name: Salary, dtype: int64 df[['Salary','Bonus']].add([3,2]) returns: Salary Bonus John 63 7 Marry 65 5 Sam 68 4 Jo 62 6
Example: Adding columns in a DataDrame
The add() function can be applied in a DataFrame to get the addition of two series/column element-wise. Consider the following example.
import pandas as pd import numpy as np df = pd.DataFrame({ "Bonus": [5, 3, 2, 4], "Salary": [60, 62, 65, 59]}, index= ["John", "Marry", "Sam", "Jo"] ) print("The DataFrame is:") print(df) #adding 'Bonus' and 'Salary' column df['Total Salary'] = df['Salary'].add(df['Bonus']) print("\nThe DataFrame is:") print(df)
The output of the above code will be:
The DataFrame is: Bonus Salary John 5 60 Marry 3 62 Sam 2 65 Jo 4 59 The DataFrame is: Bonus Salary Total Salary John 5 60 65 Marry 3 62 65 Sam 2 65 67 Jo 4 59 63
❮ Pandas DataFrame - Functions