Change language

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 )

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically