Pandas DataFrame.pct_change(~) 计算 DataFrame 每一列的连续值之间的百分比变化。
参数
1.periods | int | optional
如果是 periods=2 ,则将使用后面两行的值计算百分比变化。默认情况下, periods=1 ,这意味着前一行中的值将用于计算百分比变化。
2. fill_method | string | optional
填补缺失值的规则:
| 值 | 说明 | 
|---|---|
| 
 | 使用下一个非 | 
| 
 | 使用之前的非 | 
默认情况下,fill_method="pad" 。
警告
无论 fill_method 如何,第一行始终具有 NaN,因为没有先前的值来计算百分比变化。
3. limit | int | optional
停止填充之前要填充的连续NaN的数量。默认情况下,limit=None 。
4. freq | string 或 timedelta 或 DateOffset | optional
当 DataFrame 是时间序列时使用的时间间隔。默认情况下,freq=None 。
返回值
DataFrame 保存每列中值的百分比变化。
例子
基本用法
考虑以下 DataFrame :
df = pd.DataFrame({"A":[2,4,12], "B":[1,3,15]})
df
   A   B
0  2   1
1  4   3
2  12  15要计算 df 中每列的连续值的百分比变化:
df.pct_change()
   A    B
0  NaN  NaN
1  1.0  2.0
2  2.0  4.0在此,请注意以下事项:
- 
第一行始终是 NaN,因为没有用于计算百分比变化的先前值。
- 
为了解释如何计算这些百分比变化,请以右下角的值 ( 4.0) 为例。该值的计算方法是取df中先前值 (15-3=12) 之间的差值,然后将该差值除以先前值 (12/3=4.0)。
指定期间
考虑以下 DataFrame :
df = pd.DataFrame({"A":[2,4,12], "B":[1,3,15]})
df
   A   B
0  2   1
1  4   3
2  12  15默认情况下, periods=1 ,这意味着前一行用于计算百分比变化:
df.pct_change()   # periods=1
   A    B
0  NaN  NaN
1  1.0  2.0
2  2.0  4.0要使用前 2 行中的值计算百分比变化:
df.pct_change(periods=2)
   A    B
0  NaN  NaN
1  NaN  NaN
2  5.0  14.0我们得到第二行的NaN,因为没有可以比较的行。
注意
要使用后续行来计算百分比变化,请设置 periods=-1 。
指定fill_method
考虑以下带有一些缺失值的DataFrame:
df = pd.DataFrame({"A":[2,pd.np.nan,12], "B":[1,3,pd.np.nan]})
df
   A     B
0  2.0   1.0
1  NaN   3.0
2  12.0  NaN软填充
默认,fill_method="pad",这意味着以前的非NaNvalue 用于填充NaN:
df.pct_change()   # fill_method="pad"
   A    B
0  NaN  NaN
1  0.0  2.0
2  5.0  0.0请注意,这相当于在以下内容上调用pct_change():
pd.DataFrame({"A":[2,2,12], "B":[1,3,3]})
   A   B
0  2   1
1  2   3
2  12  3警告
无论 fill_method 为何,第一行始终具有 NaN,因为没有用于计算百分比变化的先前值。
填充
填写NaN使用下一个非NaN DataFrame 中的值:
df.pct_change(fill_method="bfill")
   A    B
0  NaN  NaN
1  5.0  2.0
2  0.0  NaN请注意,这相当于在以下内容上调用pct_change():
pd.DataFrame({"A":[2,12,12], "B":[1,3,pd.np.NaN]})
   A   B
0  2   1
1  12  3
2  12  NaN请注意右下角的 NaN 仍然是 NaN - 这是因为下一行中不存在非 NaN(没有下一行)。
指定频率
考虑以下时间序列 DataFrame:
idx = pd.date_range(start="2020-12-20", periods=4)
df = pd.DataFrame({"A":[2,4,12,24], "B":[1,3,15,30]}, index=idx)
df
            A  B
2020-12-20  2  1
2020-12-21  4  3
2020-12-22  12  15
2020-12-23  24  30计算每 2 天的百分比变化(例如 12-20 和 12-22 ):
df.pct_change(freq="2D")
            A    B
2020-12-20  NaN  NaN
2020-12-21  NaN  NaN
2020-12-22  5.0  14.0
2020-12-23  5.0  9.0在这里,我们获得前 2 行的 NaN 值,因为没有用于计算百分比变化的日期 12-18 和 12-19。
相关用法
- Python Pandas DataFrame pop方法用法及代码示例
- Python Pandas DataFrame pow方法用法及代码示例
- Python Pandas DataFrame pipe方法用法及代码示例
- Python PySpark DataFrame printSchema方法用法及代码示例
- Python Pandas DataFrame product方法用法及代码示例
- Python Pandas DataFrame empty属性用法及代码示例
- Python Pandas DataFrame nsmallest方法用法及代码示例
- Python Pandas DataFrame sample方法用法及代码示例
- Python Pandas DataFrame items方法用法及代码示例
- Python Pandas DataFrame max方法用法及代码示例
- Python Pandas DataFrame swaplevel方法用法及代码示例
- Python Pandas DataFrame agg方法用法及代码示例
- Python Pandas DataFrame copy方法用法及代码示例
- Python Pandas DataFrame insert方法用法及代码示例
- Python Pandas DataFrame lt方法用法及代码示例
- Python Pandas DataFrame all方法用法及代码示例
- Python Pandas DataFrame unstack方法用法及代码示例
- Python Pandas DataFrame mean方法用法及代码示例
- Python PySpark DataFrame filter方法用法及代码示例
- Python Pandas DataFrame tz_convert方法用法及代码示例
- Python Pandas DataFrame isin方法用法及代码示例
- Python PySpark DataFrame collect方法用法及代码示例
- Python PySpark DataFrame intersect方法用法及代码示例
- Python PySpark DataFrame dtypes属性用法及代码示例
- Python Pandas DataFrame rank方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 Pandas DataFrame | pct_change method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
