Pandas 数据帧pct_change()方法

原文:https://www.studytonight.com/pandas/pandas-dataframe-pct_change-method

百分比变化在数据分析中很有用,如制作报告或计算月与月或年与年之间的销售差异等。

Python pandas 有一个名为DataFrame.pct_change()的方法,可以计算当前元素和先前元素之间数据帧的百分比变化。在本教程中,我们将通过解决例子来讨论和学习DataFrame.pct_change()方法。

下面是DataFrame.pct_change()方法的语法。

句法

DataFrame.pct_change(periods=1, fill_method='pad', limit=None, freq=None, **kwargs)

因素

句点:表示 int,默认值为 1。它指示需要移动多少个周期来计算百分比变化。

fill_method: 表示字符串,默认为‘pad’方法。它指示在计算百分比变化之前如何处理空值或缺失值。

极限:表示 int,默认值为 None。它指示在停止之前要填充多少个连续的空值。

freq: 表示日期偏移量、时间增量或字符串,可选。

**kwargs :表示附加的关键字参数传入 DataFrame.shift 或 Series.shift。

例 1:计算 Pandas 的百分比变化

让我们使用时间序列作为索引创建一个数据帧,并使用DataFrame.pct_change()方法计算百分比变化。

在输出中,我们可以看到第一行包含空值,因为没有前一行来计算百分比变化。正值表示百分比增加,负值表示百分比减少。见下面的例子

import pandas as pd
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2,751.23,852.21],'Tea': [700.21,695.21,726.21],'Pepper':[900.14,8254.1,455.27]}, index=Values)
print("----------The dataset is----------")
print(df)
print("-------percentage change in the dataset-------")
print(df.pct_change())

-数据集为- 咖啡茶椒 2021-01-03 755.20 700.21 900.14 2021-02-07 751.23 695.21 8254.10 2021-03-14 852.21 726.21 455.27 -数据集内百分比变化- 咖啡茶椒【T7

例 1:计算 Pandas 的百分比变化

让我们使用时间序列作为索引创建一个数据帧,并使用DataFrame.pct_change()方法沿列轴计算百分比变化。

import pandas as pd
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2,751.23,852.21],'Tea': [700.21,695.21,726.21],'Pepper':[900.14,8254.1,455.27]}, index=Values)
print("----------The dataset is----------")
print(df)
print("-------percentage change in the dataset-------")
print(df.pct_change(axis=1))

-数据集为- 1 月 2 月 3 月 2021-01-03 755.20 700.21 900.14 2021-02-07 751.23 695.21 8254.10 2021-03-14 852.21 726.21 455.27 -数据集百分比变化- 1 月 2 月 3 日【T7

例 3:计算 Pandas 的百分比变化

这里,在这个例子中,我们通过DataFrame.pct_change()方法中的参数period=2来计算百分比。

import pandas as pd
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2,751.23,852.21],'Tea': [700.21,695.21,726.21],'Pepper':[900.14,8254.1,455.27]}, index=Values)
print("----------The dataset is----------")
print(df)
print("-------percentage change in the dataset-------")
print(df.pct_change(periods=2))

-数据集为- 咖啡茶椒 2021-01-03 755.20 700.21 900.14 2021-02-07 751.23 695.21 8254.10 2021-03-14 852.21 726.21 455.27 -数据集内百分比变化- 咖啡茶椒【T7

例 4:计算 Pandas 的百分比变化

在计算百分比变化之前,我们可以处理数据帧中的任何缺失值。我们可以将参数fill_method=pad 传递给DataFrame.pct_change()方法,该方法向前填充空值。

import pandas as pd
Values = pd.date_range('2021-01-01', periods=3, freq='5W')
df = pd.DataFrame({'coffee': [755.2,751.23,852.21],'Tea': [700.21,695.21,726.21],'Pepper':[900.14,8254.1,455.27]}, index=Values)
print("----------The dataset is----------")
print(df)
print("-------percentage change in the dataset-------")
print(df.pct_change(fill_method='ffill'))

-数据集为- 咖啡茶椒 2021-01-03 755.20 700.21 900.14 2021-02-07 751.23 695.21 8254.10 2021-03-14 852.21 726.21 455.27 -数据集内百分比变化- 咖啡茶椒【T7

结论

在本教程中,我们学习了 PandasDataFrame.pct_change()方法。我们学习了语法和参数,并将其应用于数据帧,以理解 DataFrame.pct_change()方法。