Python Pandas dataframe.rmul()

Python Pandas dataframe.rmul()

Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python软件包的奇妙生态系统。Pandas就是这些包中的一个,使导入和分析数据变得更加容易。

Pandas dataframe.rmul()函数用于寻找dataframe和其他元素的乘法(二元运算rfloordiv)。这个函数本质上与做其他*数据帧相同,但支持替代其中一个输入中的缺失数据。

语法: DataFrame.rmul(other, axis=’columns’, level=None, fill_value=None)
参数 :
other:系列,数据框架,或常数
axis:对于系列输入,axis要与系列的索引相匹配。
level :跨层广播,与通过的MultiIndex层的索引值相匹配。
fill_value : 在计算前用这个值填充现有的缺失(NaN)值,以及成功的DataFrame对齐所需的任何新元素。如果两个对应的DataFrame位置的数据都缺失,那么结果将是缺失。
返回 : result : DataFrame

例子#1:使用rmul()函数来寻找一个系列与一个数据帧的乘法。

# 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]},
                    index =["A1", "A2", "A3", "A4", "A5"])
 
# Print the dataframe
df
Python

Python Pandas dataframe.rmul()

让我们来创建这个系列

# importing pandas as pd
import pandas as pd
 
# Create the series
sr = pd.Series([12, 25, 64, 18], index =["A", "B", "C", "D"])
 
# Print the series
sr
Python

Python Pandas dataframe.rmul()

让我们使用dataframe.rmul()函数来寻找一个系列与dataframe的乘法。

df.rmul(sr, axis = 1)
Python

输出 :

Python Pandas dataframe.rmul()

例子#2:使用rmul()函数来执行一个数据帧与其他数据帧的乘法。

# 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]},
                    index =["A1", "A2", "A3", "A4", "A5"])
 
# Creating the second dataframe
df2 = pd.DataFrame({"A":[10, 11, 7, 8, 5],
                    "B":[21, 5, 32, 4, 6],
                    "C":[11, 21, 23, 7, 9],
                    "D":[1, 5, 3, 8, 6]},
                     index =["A1", "A2", "A3", "A4", "A5"])
 
# Print the first dataframe
print(df1)
 
# Print the second dataframe
print(df2)
Python

Python Pandas dataframe.rmul()

让我们执行df2 * df1

# perform multiplication of df2 with df1
df1.rmul(df2)
Python

输出 :

Python Pandas dataframe.rmul()

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册