Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python pandas percent change with columns of dataframe

I just started studying pandas and have questions. Firstly, I'd like to ask this.

I have dataframe and it's like below.

  Date        Open        High         Low       Close    
2015-11-02  711.059998  721.619995  705.849976  721.109985   
2015-11-03  718.859985  724.650024  714.719971  722.159973   
2015-11-04  722.000000  733.099976  721.900024  728.109985   
2015-11-05  729.469971  739.479980  729.469971  731.250000   
2015-11-06  731.500000  735.409973  727.010010  733.760010   

I know

df["Close"].pct_change() 

make the percent change from Close to Close.

But, I want to add a new column, "CloseToOpen" which is a percent change of "yesterday Close to today Open".

So, it is "Open(Day 0) / Close(Day -1) -1". Of course, the first row should be "NaN" or Zero because there's no "previous day's Close".

How can I make this with python pandas code??

Thanks guys!

This is what I want.

Date        Open        High        Low         Close       CloseToOpen
2015-11-02  711.059998  721.619995  705.849976  721.109985  0.000000
2015-11-03  718.859985  724.650024  714.719971  722.159973  -0.003120
2015-11-04  722.000000  733.099976  721.900024  728.109985  -0.000222
2015-11-05  729.469971  739.479980  729.469971  731.250000  0.001868
2015-11-06  731.500000  735.409973  727.010010  733.760010  0.000342
like image 745
B.A. Avatar asked Feb 12 '18 09:02

B.A.


People also ask

How to calculate percent change in pandas Dataframe?

Pandas dataframe.pct_change() function calculates the percentage change between the current and a prior element. This function by default calculates the percentage change from the immediately previous row. Note : This function is mostly useful in the time-series data. Parameters : periods : Periods to shift for forming percent change.

What is PCT_change in pandas?

Pandas’ pct_change () function will compute percent change for each value in a column when compared to the previous element in the column by default. Another way to think is Computes the percentage change from the immediately previous row

How to do data analysis in Python with pandas?

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas dataframe.pct_change () function calculates the percentage change between the current and a prior element.

How to find percentage change in data with NaN values?

The first row contains NaN values, as there is no previous row from which we can calculate the change. Example #2: Use pct_change () function to find the percentage change in the data which is also having NaN values. The first row contains NaN values, as there is no previous row from which we can calculate the change.


1 Answers

Also you can use standard operands to achieve what you wanted:

df['CloseToOpen'] = (df['Open'] / df['Close'].shift(1) - 1).fillna(0)
like image 160
zipa Avatar answered Sep 22 '22 18:09

zipa