search
Search
Login
Math ML Join our weekly DS/ML newsletter
menu
menu search toc more_vert
Robocat
Guest 0reps
Thanks for the thanks!
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
help Ask a question
Share on Twitter
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to
A
A
brightness_medium
share
arrow_backShare
Twitter
Facebook

Pandas DataFrame | radd method

Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Binary Operators
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags

Pandas DataFrame.radd(~) method computes and returns the sum of a scalar, sequence, Series or DataFrame and the values in the source DataFrame, that is:

other + DataFrame

Note that this is the reverse of DataFrame.add(~), which does the following:

DataFrame + other
NOTE

Unless you use the parameters axis, level and fill_value, radd(~) is equivalent to performing addition using the + operator.

Parameters

1. otherlink | scalar or sequence or Series or DataFrame

The resulting DataFrame will be the sum of other and the source DataFrame.

2. axislink | int or string | optional

Whether to broadcast other for each column or row of the source DataFrame:

Axis

Description

other is broadcasted for each column.

"index" or 0

other is broadcasted for each row.

"columns" or 1

This is relevant only when the shape of the source DataFrame and other does not align. By default, axis=1.

3. level | int or string | optional

The name or the integer index of the level to consider. This is relevant only if your DataFrame is Multi-index.

4. fill_valuelink | float or None | optional

The value to replace NaN before the computing the sum. If both the pair-wise entries in the source DataFrame and other are NaN, then the resulting sum will still be NaN. By default, fill_value=None.

Return Value

A new DataFrame computed by the sum of the source DataFrame and other.

Examples

Basic usage

Consider the following DataFrames:

df = pd.DataFrame({"A":[2,3], "B":["a","b"]})
df_other = pd.DataFrame({"A":[6,7], "B":["c","d"]})
A B | A B
0 2 a | 0 6 c
1 3 b | 1 7 d

Taking the sum yields:

df.radd(df_other)
A B
0 8 ca
1 10 db

Broadcasting

Consider the following DataFrame:

df = pd.DataFrame({"A":[2,3], "B":[4,5]})
df
A B
0 2 4
1 3 5

Row-wise addition

By default, axis=1, which means that other will be broadcasted for each row in df:

df.radd([10,20]) # axis=1
A B
0 12 24
1 13 25

Here, we're doing the following element-wise addition:

10+2 20+4
10+3 20+5

Column-wise addition

To broadcast other for each column in df, set axis=0 like so:

df.radd([10,20], axis=0)
A B
0 12 14
1 23 25

Here, we're doing the following element-wise addition:

10+2 10+4
20+3 20+5

Specifying fill_value

Consider the following DataFrames:

df = pd.DataFrame({"A":[2,np.NaN], "B":[np.NaN,5]})
df_other = pd.DataFrame({"A":[10, 20],"B":[np.NaN,np.NaN]})
A B | A B
0 2 NaN | 0 10 NaN
1 NaN 5 | 1 20 NaN

By default, when we take the sum using radd(~), any operation with NaN results in NaN:

df.radd(df_other)
A B
0 2.0 NaN
1 NaN NaN

We can fill the NaN values before we compute the sum by using the fill_value parameter:

df.radd(df_other, fill_value=100)
A B
0 12.0 NaN
1 120.0 105.0

Notice when the addition is between two NaN, the resulting sum would still be a NaN regardless of fill_value.

mail
Join our newsletter for updates on new DS/ML comprehensive guides (spam-free)
robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Ask a question or leave a feedback...
thumb_up
0
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!