📜  Python|熊猫系列.pow()

📅  最后修改于: 2022-05-13 01:54:18.906000             🧑  作者: Mango

Python|熊猫系列.pow()

Python是一种用于进行数据分析的出色语言,主要是因为以数据为中心的Python包的奇妙生态系统。 Pandas就是其中之一,它使导入和分析数据变得更加容易。

Pandas Series.pow()是一系列数学运算方法。这用于将传递的系列的每个元素作为调用者系列的指数幂并返回结果。为此,两个系列的索引必须相同,否则返回错误。

示例 #1:
在此示例中,使用 Pandas .Series() 方法创建了两个系列。该系列中没有一个具有空值。第二个系列直接作为其他参数传递,以返回操作后的值。

# importing pandas module
import pandas as pd
  
# creating first series
first =[1, 2, 5, 6, 3, 4]
  
# creating second series
second =[5, 3, 2, 1, 3, 2]
  
# making series
first = pd.Series(first)
  
# making series
second = pd.Series(second)
  
# calling .pow()
result = first.pow(second)
  
# display
result

输出:
如输出所示,返回值等于第一个系列,第二个系列为其指数幂。

0     1
1     8
2    25
3     6
4    27
5    16
dtype: int64


示例 #2:处理 Null 值

在此示例中,NaN 值也使用 numpy.nan 方法放入系列中。之后将 2 传递给 fill_value 参数以用 2 替换空值。

# importing pandas module
import pandas as pd
  
# importing numpy module
import numpy as np
  
# creating first series
first =[1, 2, 5, 6, 3, np.nan, 4, np.nan]
  
# creating second series
second =[5, np.nan, 3, 2, np.nan, 1, 3, 2]
  
# making series
first = pd.Series(first)
  
# making seriesa
second = pd.Series(second)
  
# value for null replacement
null_replacement = 2
  
# calling .pow()
result = first.pow(second, fill_value = null_replacement)
  
# display
result

输出:
如输出所示,在操作之前,所有 NaN 值都被 2 替换,并且返回的结果中没有任何 Null 值。

0      1.0
1      4.0
2    125.0
3     36.0
4      9.0
5      2.0
6     64.0
7      4.0
dtype: float64