📜  Python|熊猫 dataframe.pct_change()

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

Python|熊猫 dataframe.pct_change()

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

Pandas dataframe.pct_change()函数计算当前元素和先前元素之间的百分比变化。默认情况下,此函数计算前一行的百分比变化。

注意:此函数在时间序列数据中最有用。

示例 #1:使用pct_change()函数查找时间序列数据的百分比变化。

# importing pandas as pd
import pandas as pd
  
# Creating the time-series index
ind = pd.date_range('01/01/2000', periods = 6, freq ='W')
  
# Creating the dataframe 
df = pd.DataFrame({"A":[14, 4, 5, 4, 1, 55],
                   "B":[5, 2, 54, 3, 2, 32], 
                   "C":[20, 20, 7, 21, 8, 5],
                   "D":[14, 3, 6, 2, 6, 4]}, index = ind)
  
# Print the dataframe
df

让我们使用dataframe.pct_change()函数来查找数据的百分比变化。

# find the percentage change with the previous row
df.pct_change()

输出 :

第一行包含NaN值,因为没有前一行我们可以从中计算变化。示例 #2:使用pct_change()函数查找数据中也具有NaN值的百分比变化。

# importing pandas as pd
import pandas as pd
  
# Creating the time-series index
ind = pd.date_range('01/01/2000', periods = 6, freq ='W')
  
# Creating the dataframe 
df = pd.DataFrame({"A":[14, 4, 5, 4, 1, 55],
                   "B":[5, 2, None, 3, 2, 32], 
                   "C":[20, 20, 7, 21, 8, None],
                   "D":[14, None, 6, 2, 6, 4]}, index = ind)
  
# apply the pct_change() method
# we use the forward fill method to
# fill the missing values in the dataframe
df.pct_change(fill_method ='ffill')

输出 :

第一行包含NaN值,因为没有前一行我们可以从中计算变化。数据框中的所有NaN值都已使用ffill方法填充。