天天看点

第九章:数据聚合与分组运算3、分组级运算和转换

说明:本文章为Python数据处理学习日志,记录内容为实现书本内容时遇到的错误以及一些与书本不一致的地方,一些简单操作则不再赘述。日志主要内容来自书本《利用Python进行数据分析》,Wes McKinney著,机械工业出版社。

3、分组级运算和转换

P285示例

pct_change()

Signature: DataFrame.pct_change(self, periods=1, fill_method=’pad’, limit=None, freq=None, **kwargs)

Docstring Percent change over given number of periods.

Parameters

periods : int, default 1

Periods to shift for forming percent change

fill_method : str, default ‘pad’

How to handle NAs before computing percent changes

limit : int, default None

The number of consecutive NAs to fill before stopping

freq : DateOffset, timedelta, or offset alias string, optional

Increment to use from time series API (e.g. ‘M’ or BDay())

Returns

chg : NDFrame

这个函数运算方法如下:

test = DataFrame([,,,,])

test['pct_change'] = test.pct_change()

test
Out[]: 
     pct_change
           NaN  #第一个数据没有
            #(2-1)/1=1
            #(4-2)/2=1
            #(4-4)/4=0
            #(5-4)/4=0

test['pct_change'] = test.pct_change(periods=)

test
Out[]: 
     pct_change
           NaN
           NaN  #前两个没有
            #(4-1)/1=3
            #(4-2)/2=1
            #(5-4)/4=0.25