Fetch column from datafrom and use as array by python pandas -


i want fetch column dataframe array.

then make new array array attach original dataframe.

my dataframe(df) this.

   close        per 0  637.0  -0.156740 1  638.0    0.789889 2  633.0   -1.555210 3  643.0   -1.832061 4  655.0    0.000000 

then try fetch column dataframe , make moving average.

newarray =  pandas.rollling_mean(df.per,3) ## fetch array 

then want attach original datafrom

df[sma] = newarray 

what want this

   close    per       sma 0  637.0   -0.156740  636  1  638.0    0.789889  638 2  633.0   -1.555210  643.6 3  643.0   -1.832061  nan  4  655.0    0.000000  nan 

however think there comfusion dataframe , array.

maybe might misunderstanding.

how can correct this??

depending on goals:

in [39]: df['sma'] = df.close.rolling(3).mean()  in [40]: df out[40]:    close       per         sma 0  637.0 -0.156740         nan 1  638.0  0.789889         nan 2  633.0 -1.555210  636.000000 3  643.0 -1.832061  638.000000 4  655.0  0.000000  643.666667 

or:

in [36]: df['sma'] = df.close.rolling(3, center=true).mean().shift(-1)  in [37]: df out[37]:    close       per         sma 0  637.0 -0.156740  636.000000 1  638.0  0.789889  638.000000 2  633.0 -1.555210  643.666667 3  643.0 -1.832061         nan 4  655.0  0.000000         nan 

Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -