简体   繁体   中英

pct_change for column value

Using Pandas documentation

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

 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

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

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:

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

Apply pct_change to single/multiple column(s), in a data frame can be done as below

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_change to a single column ( 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 columns as 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM