Python|熊猫 dataframe.rdiv()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.rdiv()
函数计算数据帧和其他元素的浮点除法(二元运算符rtruediv)。其他对象可以是标量、熊猫系列或熊猫数据框。此函数本质上与执行other / dataframe
相同,但支持用 fill_value 替换其中一个输入中的缺失数据。
Syntax: DataFrame.rdiv(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
numeric_only : Include only float, int, boolean data. Valid only for DataFrame or Panel objects
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:使用rdiv()
函数将系列与数据框元素分开
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df = pd.DataFrame({"A":[1, 5, 3, 4, 2],
"B":[3, 2, 4, 3, 4],
"C":[2, 2, 7, 3, 4],
"D":[4, 3, 6, 12, 7]})
# Print the dataframe
df
让我们创建一个系列
# importing pandas as pd
import pandas as pd
# Create a series
sr = pd.Series([5, 10, 15, 20], index =["A", "B", "C", "D"])
# Print the series
sr
让我们使用dataframe.rdiv()
函数用数据框划分系列
# perform division of series with
# dataframe element-wise over the column axis
df.rdiv(sr, axis = 1)
输出 :
示例 #2:使用rdiv()
函数将一个数据帧与另一个包含NaN
值的数据帧分开。
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df1 = pd.DataFrame({"A":[1, 5, 3, 4, 2],
"B":[3, 2, 4, 3, 4],
"C":[2, 2, 7, 3, 4],
"D":[4, 3, 6, 12, 7]})
# Creating the second dataframe
df2 = pd.DataFrame({"A":[14, 5, None, 4, 12],
"B":[7, 6, 4, 5, None],
"C":[2, 11, 4, 3, 6],
"D":[4, None, 6, 2, 4]})
# divide df2 by df1 element-wise
# Fill all the missing values by 100
df1.rdiv(df2, fill_value = 100)
输出 :