Python | Pandas Series.mod()
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Python Series.mod()
is used to return the remainder after division of two numbers
Syntax: Series.mod(other, axis='columns', level=None, fill_value=None)
Parameters: other: other series or list type to be divided and checked for remainder with caller series
fill_value: Value to be replaced by NaN in series/list before operation level: integer value of level in case of multi indexReturn type: Caller series with mod values ( caller series [i] % other series [i] )
To download the data set used in following example, click here.
In the following examples, the data frame used contains data of some NBA players. The image of data frame before any operations is attached below.
Example #1: Checking remainder
In this example, 5 rows of data frame are extracted usinghead()
method. A series is created from a Python list using Pandas Series()
method. The mod()
method is called on new short data frame and the created list is passed as other parameter.
# importing pandas module
import pandas as pd
# reading csv file from url
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")
# creating short data of 5 rows
short_data = data.head()
# creating list with 5 values
list =[1, 2, 3, 4, 3]
# finding remainder
# creating new column
short_data["Remainder"]= short_data["Salary"].mod(list)
# display
short_data

mod()
method.
# importing pandas module
import pandas as pd
# reading csv file from url
data = pd.read_csv("https://media.geeksforgeeks.org/wp-content/uploads/nba.csv")
# creating short data of 5 rows
short_data = data.head()
# creating list with 5 values
list =[1, 2, 3, 4, 3]
# replacing null value with any number
null_replacement = 21218
# finding remainder
# creating new column
short_data["Remainder"]= short_data["Salary"].mod(list, fill_value = null_replacement)
# display
short_data
