Python|熊猫 dataframe.pow()
Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。
Pandas dataframe.pow()
函数计算数据帧和其他元素的指数幂(二元运算符pow)。此函数与dataframe ** other
基本相同,但支持在其中一个输入数据中填充缺失值。
Syntax: DataFrame.pow(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.
**kwargs : Additional keyword arguments are passed into DataFrame.shift or Series.shift.
Returns : result : DataFrame
示例 #1:使用pow()
函数查找数据框中每个元素的幂。使用系列将连续的每个元素提升到不同的幂。
# importing pandas as pd
import pandas as pd
# Creating the dataframe
df1 = pd.DataFrame({"A":[14, 4, 5, 4, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 20, 7, 3, 8],
"D":[14, 3, 6, 2, 6]})
# Print the dataframe
df
让我们创建一个系列
# importing pandas as pd
import pandas as pd
# Create the Series
sr = pd.Series([2, 3, 4, 2], index =["A", "B", "C", "D"])
# Print the series
sr
现在,让我们使用dataframe.pow()
函数将一行中的每个元素提升到不同的幂。
# find the power
df.pow(sr, axis = 1)
输出 :
示例#2:使用pow()
函数将第一个数据帧的每个元素提升到另一个数据帧中相应元素的幂。
# importing pandas as pd
import pandas as pd
# Creating the first dataframe
df1 = pd.DataFrame({"A":[14, 4, 5, 4, 1],
"B":[5, 2, 54, 3, 2],
"C":[20, 20, 7, 3, 8],
"D":[14, 3, 6, 2, 6]})
# Creating the second dataframe
df2 = 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]})
# using pow() function to raise each element
# in df1 to the power of corresponding element in df2
df1.pow(df2)
输出 :