search
Search
Login
Map of Data Science
menu
menu search toc more_vert
Robocat
Guest 0reps
Sign up
Log in
account_circleMy Profile homeAbout paidPricing
emailContact us
exit_to_appLog out
Map of data science
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 | truediv method

Pandas
chevron_right
Documentation
chevron_right
DataFrame
chevron_right
Binary Operators
schedule Jul 1, 2022
Last updated
local_offer PythonPandas
Tags
map
Check out the interactive map of data science

Pandas DataFrame.truediv(~) method divides the values in the source DataFrame by a scalar, sequence, Series or DataFrame, that is:

DataFrame / other
NOTE

Unless you use the parameters axis, level and fill_value, the truediv(~) is equivalent to performing division using the / operator. Also, truediv(~) is equivalent to div(~).

Parameters

1. otherlink | scalar or sequence or Series or DataFrame

The resulting DataFrame will be the source DataFrame divided by other.

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 only relevant when the shape of the source DataFrame and that of 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 computation. Note that division between two NaN still results in NaN. By default, fill_value=None.

Return Value

A new DataFrame resulting from the division.

Examples

Basic usage

Consider the following DataFrames:

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

Performing true division yields:

df.truediv(df_other)
A B
0 2.0 0.040
1 0.3 0.005

Note that this is equivalent to:

df/ df_other
A B
0 2.0 0.040
1 0.3 0.005

Broadcasting

Consider the following DataFrame:

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

Row-wise division

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

df.truediv([1,10]) # axis=1
A B
0 2.0 0.4
1 3.0 0.5

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

2/1 4/10
3/1 5/10

Column-wise division

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

df.truediv([1,10], axis=0)
A B
0 2.0 4.0
1 0.3 0.5

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

2/1 4/1
3/10 5/10

Specifying fill_value

Consider the following DataFrames:

df = pd.DataFrame({"A":[12,20],"B":[np.NaN,np.NaN]})
df_other = pd.DataFrame({"A":[3,np.NaN], "B":[np.NaN,4]})
A B | A B
0 3.0 NaN | 0 12 NaN
1 NaN 4.0 | 1 20 NaN

By default, when we compute the division using truediv(~), any operation with NaN results in NaN:

df.truediv(df_other)
A B
0 4.0 NaN
1 NaN NaN

We can fill the NaN values before we perform division by using the fill_value parameter:

df.truediv(df_other, fill_value=2)
A B
0 4.0 NaN
1 10.0 0.5

Here, notice how the division between two NaN still results in NaN regardless of fill_value.

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!