Akiru
Akiru

Reputation: 319

Optimal way to iterate full pandas DataFrame

I have an Stocks CSV and I would like to add a column with the difference in percent.

For example:

Original csv:

Date; Price;
Jan; 60.1;
Feb; 59.4;
Mar; 65.9;

And what I want is to create a new pandas DataFrame column including the change in %, for example:

Date; Price; Change %;
Jan; 60.1;1.18;
Feb; 59.4;-9.86;
Mar; 65.9;;

In other languages I would normally do this by iterating the data with FOR loops, using the FOR index for selecting the desired row. Something like "Change[i]=((Price[i]/Price[i-1])-1)*100" But my question is; Is there any way to do this in a simpler and optimal way? Like Apply() function in R ?

Greetings!

Upvotes: 0

Views: 52

Answers (1)

danie
danie

Reputation: 165

Pandas has a DataFrame method that does exactly that: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.pct_change.html (then just multiply by 100 if you want to).

Alternatively, you can just use DataFrame.shift() and do:

df['Change_pct'] = df['Price']/df['Price'].shift(1)*100

Upvotes: 4

Related Questions