search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
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

Pandas DataFrame | mod method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas DataFrame.mod(~) method computes the modulo of the values in the source DataFrame and another scalar, sequence, Series or DataFrame, that is:

DataFrame % other
NOTE

Unless you use the parameters axis, level and fill_value, mod(~) is equivalent to computing the modulo using the % operator.

Parameters

1. otherlink | scalar or sequence or Series or DataFrame

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

2. axislink | int or string | optional

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

Axis

Description

"index" or 0

other is broadcasted for each column.

"columns" or 1

other is broadcasted for each row.

This is only relevant if 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 of the modulo. If both the entries in the source DataFrame and the other are NaN, then the result for that entry will still be NaN. By default, fill_value=None.

Return Value

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

Examples

Basic usage

Consider the following DataFrames:

df = pd.DataFrame({"A":[12,13], "B":[24,25]})
df_other = pd.DataFrame({"A":[4,5], "B":[7,8]})
A B | A B
0 12 24 | 0 4 7
1 13 25 | 1 5 8

Computing the modulo:

df.mod(df_other)
A B
0 0 3
1 3 1

Note that this is equivalent to:

df % df_other
A B
0 0 3
1 3 1

Broadcasting

Consider the following DataFrame:

df = pd.DataFrame({"A":[12,13], "B":[24,25]})
df
A B
0 12 24
1 13 25

Row-wise addition

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

df.mod([10,20]) # axis=1
A B
0 2 4
1 3 5

Here, we're computing the following element-wise modulo:

12%10 24%20
13%10 25%20

Column-wise addition

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

df.mod([10,20], axis=0)
A B
0 2 4
1 13 5

Here, we're computing the following element-wise modulo:

12%10 24%10
13%20 25%20

Specifying fill_value

Consider the following DataFrames:

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

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

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

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

df.mod(df_other, fill_value=5)
A B
0 2.0 NaN
1 5.0 0.0

Notice how the operation between two NaN still results in a NaN, regardless of fill_value.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
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!