Python|熊猫 dataframe.mod()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.mod()函数返回数据帧和其他元素的模数(二元运算符mod)。此函数与 Dataframe % other 基本相同,但支持用 fill_value 替换其中一个输入中的缺失数据。此函数可用于系列或数据框。
Syntax: DataFrame.mod(other, axis=’columns’, level=None, fill_value=None)
Parameters :
Other : Series, DataFrame, or constant
axis : For Series input, axis to match Series index on
level : Broadcast across a level, matching Index values on the passed MultiIndex level
fill_value : Fill existing missing (NaN) values, and any new element needed for successful DataFrame alignment, with this value before computation. If data in both corresponding DataFrame locations is missing the result will be missing
Returns : result : DataFrame
示例 #1:使用 mod()函数查找数据帧中每个值的模数和常数。
Python3
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[12, 4, 5, 44, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 16, 7, 3, 8],
"D":[14, 3, 17, 2, 6]})
# Print the dataframe
df
Python3
# find mod of dataframe values with 3
df.mod(3)
Python3
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[12, 4, 5, 44, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 16, 7, 3, 8],
"D":[14, 3, 17, 2, 6]})
# Print the dataframe
df
Python3
# create a series
sr = pd.Series([3, 2, 4, 5])
# setting its column index similar to the dataframe
sr.index =["A", "B", "C", "D"]
# print the series
sr
Python3
# find mod of dataframe values with series
# axis = 1 indicates column axis
df.mod(sr, axis = 1)
让我们使用 dataframe.mod()函数来找到数据帧与 3 的模数
Python3
# find mod of dataframe values with 3
df.mod(3)
输出 :
示例 #2:使用 mod()函数在列轴上找到具有系列的模数。
Python3
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[12, 4, 5, 44, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 16, 7, 3, 8],
"D":[14, 3, 17, 2, 6]})
# Print the dataframe
df
让我们创建系列对象
Python3
# create a series
sr = pd.Series([3, 2, 4, 5])
# setting its column index similar to the dataframe
sr.index =["A", "B", "C", "D"]
# print the series
sr
让我们使用 dataframe.mod()函数来找到数据帧与系列的模数
Python3
# find mod of dataframe values with series
# axis = 1 indicates column axis
df.mod(sr, axis = 1)
输出 :