Python | Pandas dataframe.add ()



Dataframe.add () is used to add data and other elements element by element (binary add operator). Equivalent to dataframe + other, but with support for fill_value replacement for missing data in one of the inputs.

Syntax: DataFrame.add (other, axis = `columns`, level = None, fill_value = None)

Parameters:

other: Series, DataFrame, or constant
axis: {0, 1, `index`, `columns`} For Series input, axis to match Series index on
fill_value: [None or float value , default None] Fill missing (NaN) values ​​with this value. If both DataFrame locations are missing, the result will be missing.
level: [int or name] Broadcast across a level, matching Index values ​​on the passed MultiIndex level

Returns: result DataFrame

# Import pandas as pd

import pandas as pd

 
# Import numpy as np

import numpy as np

 
# Create a data frame
# Set the initial value for re-generating the result.

np.random.seed ( 25 ) < / code>

 

df = pd.DataFrame (np.random.rand ( 10 , 3 ), columns = [ ` A` , `B` , `C` ])

 
# np.random.rand (10, 3) generated
# a random 2-dimensional array of the form 10 * 3
# which is then converted to a data frame

 
df

Note: function add () is similar to operation & # 39; + & # 39;, but add () provides additional support for missing values ​​in one of the inputs.

# We need NaN values ​​in the dataframe.
# so let`s fill the last line with NaN

df.iloc [ - 1 ] = np.nan

 
df

Adding a constant value to a data frame using the add () function :

# add 1 to all elements
# data frame

df.add ( 1 )

 

Note the above output in fill_value nan of the df dataframe.add () function, the fill_value attribute was not added. This will fill in the missing value (Nan) with the assigned value. If both data values ​​are missing, there will be no result.

Let`s see how.

# We gave the default
# of & # 39; 10 & # 39; for all nanocells

df.add ( 1 , fill_value = 10 )


All nanoelements were filled first with 10 and then 1 is added to it.

Adding a series to the Dataframe:

To enter a series, the index dimension must be the same for both the data frame and the series.

# Create a series of 10 values ​​

tk = pd.Series (np.ones ( 10 ))

 
# mk is a series of 10 elements
# everything is filled in 1

# Add tk (series) to df (dataframe)
# along the index axis

df.add (tk, axis = `index` )

Adding one data frame with another data frame

# Create a second data frame
# First mouth new seeds to regenerate the result

np.random.seed ( 10 )

 
# Create a 5 * 5 data frame

df2 = pd.DataFrame (np. random.rand ( 5 , 5 ), columns = [ `A` , `B` , ` C` , `D` , `E` ])

  
df2

Let`s add these two data frames element by element

df.add (df2)

Note that the resulting information frame is 10 * 5 and nan in all those cells for which any of these frames are nan .

Let`s fix this —

# Set the default to 10 for nanocells
The # nan value will not be filled in for these cells
# in which both data frames have a value nan

df.add (df2, fill_value = 10 )