Python Pandas dataframe.rpow()
Python是一种进行数据分析的伟大语言,主要是因为以数据为中心的Python软件包的奇妙生态系统。Pandas就是这些包中的一个,使导入和分析数据变得更加容易。
Pandas dataframe.rpow()函数用于寻找数据框架和其他元素的指数幂(二进制运算rfloordiv)。这个函数本质上与做其他数据框架相同,但支持替代其中一个输入中的缺失数据。
语法: DataFrame.rpow(other, axis=’columns’, level=None, fill_value=None)
参数 :
other:系列,数据框架,或常数
axis:对于系列输入,axis要与系列的索引相匹配。
level :跨层广播,与通过的MultiIndex层的索引值相匹配。
fill_value : 在计算前用这个值填充现有的缺失(NaN)值,以及成功的DataFrame对齐所需的任何新元素。如果两个对应的DataFrame位置的数据都缺失,结果将是缺失。
返回 : result : DataFrame
例子#1:使用rpow()函数将一个系列的每个元素提升到列axis上的数据框架中的相应值。
# 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
让我们来创建这个系列
# 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
让我们使用dataframe.rpow()函数将一个系列中的每个元素提高到数据框架中相应元素的幂。
# equivalent to sr ** df
df.rpow(sr, axis = 1)
输出 :
例子#2:使用rpow()函数将一个数据框架中的每个元素提高到其他数据框架中相应元素的幂。
# 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)
让我们执行df2 df1
# raise df2 to the power of df1
df1.rpow(df2)
输出 :