pandas 列值的 pct_change

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36218337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:56:08  来源:igfitidea点击:

pct_change for column value

pythonpandasdataframe

提问by nnnnmmm

Using Pandas documentation

http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.pct_change.html

使用 Pandas 文档

http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.DataFrame.pct_change.html

I am trying to create this function to calculate percentage_change . I pass two paramters to it

我正在尝试创建此函数来计算 percent_change 。我向它传递了两个参数

 def PCT(df,n):
        d = df['Close'].pct_change(n)

Even rewriting the same code in different way give me same error

即使以不同的方式重写相同的代码也会给我同样的错误

 P = pd.Series(df['Close'].pct_change(n), name = 'PCT_' + str(n))
  1. dataframe
  2. window over which I want % change
  1. 数据框
  2. 我想要改变百分比的窗口

It throwing error

它抛出错误

  File "D:\Python Scripts\TA_Liabrary.py", line 15, in PCT
    d = df['Close'].pct_change(n)
TypeError: 'NoneType' object has no attribute '__getitem__'

Can someone please help me in this

有人可以帮助我吗

Sample data

样本数据

Index   open    high    low close   volume  adj.
1/01/2014   54.97   54.97   54.97   54.97   0   49.31993
2/01/2014   55.1    55.95   54.86   55.08   216100  49.41862
3/01/2014   54.5    55  54.16   55  392600  49.34685
6/01/2014   54.82   55.47   54.62   55.14   344500  49.47245
7/01/2014   55.06   55.17   54.27   54.35   677400  48.76365
8/01/2014   54.64   54.88   53.87   54.38   587500  48.79057
9/01/2014   54.57   54.8    54.05   54.48   466800  48.88029

回答by Deusdeorum

Why can't you use the function as it is in the documents?

为什么不能使用文档中的功能?

a = [10,12,13]
b = [12,11,14]
d = {'open': a, 'close': b}

df = DataFrame(data=d)
print(df)

  close  open
0     12    10
1     11    12
2     14    13

print(df.pct_change(1))

With a function this will be:

使用函数,这将是:

def PCT(dataf,n):
        return dataf.pct_change(n)

print(PCT(df, 1))

Both will return:

两者都会返回:

      close      open
0       NaN       NaN
1 -0.083333  0.200000
2  0.272727  0.083333

And with your sample data PCT(df['close'], 1)will return:

并且您的示例数据PCT(df['close'], 1)将返回:

Index         close
2014-01-01         NaN
2014-02-01    0.002001
2014-03-01   -0.001452
2014-06-01    0.002545
2014-07-01   -0.014327
2014-08-01    0.000552
2014-09-01    0.001839

回答by Shijith

Apply pct_changeto single/multiplecolumn(s), in a data frame can be done as below

适用pct_change单列/多列,在数据框中可以按如下方式完成

df = pd.DataFrame({
    'open': [54.97,55.1,54.5,54.82],
    'high': [54.97,55.95,55,55.47],
    'low': [54.97,54.86,54.16,54.62],
    'close': [54.97,53.08,55,55.14]},
    index=['2014-01-01', '2014-02-01', '2014-03-01','2014-04-01'])

            open    high    low     close
2014-01-01  54.97   54.97   54.97   54.97
2014-02-01  55.10   55.95   54.86   53.08
2014-03-01  54.50   55.00   54.16   55.00
2014-04-01  54.82   55.47   54.62   55.14

apply pct_changeto a single column(close)

适用pct_change于一个单个的列close

df.close = df.close.pct_change(periods = 1)
            open    high    low     close
2014-01-01  54.97   54.97   54.97   NaN
2014-02-01  55.10   55.95   54.86   -0.034382
2014-03-01  54.50   55.00   54.16   0.036172
2014-04-01  54.82   55.47   54.62   0.002545

applying to multiple columnsas below

适用于多列,如下所示

# apply pct_change to 'open' and 'close'
df[['open','close']] = df[['open','close']].pct_change(periods = 1)
            open        high    low     close
2014-01-01  NaN         54.97   54.97   NaN
2014-02-01  0.002365    55.95   54.86   -0.034382
2014-03-01  -0.010889   55.00   54.16   0.036172
2014-04-01  0.005872    55.47   54.62   0.002545