Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas pct change from initial value

I want to find the pct_change of Dew_P Temp (C) from the initial value of -3.9. I want the pct_change in a new column.

Source here:

weather = pd.read_csv('https://raw.githubusercontent.com/jvns/pandas-cookbook/master/data/weather_2012.csv')
weather[weather.columns[:4]].head()


Date/Time        Temp (C)   Dew_P Temp (C)  Rel Hum (%)
0   2012-01-01   -1.8       -3.9             86
1   2012-01-01   -1.8       -3.7             87
2   2012-01-01   -1.8       -3.4             89
3   2012-01-01   -1.5       -3.2             88
4   2012-01-01   -1.5       -3.3             88

I have tried variations of this for loop (even going as far as adding an index shown here) but to no avail:

for index, dew_point in weather['Dew_P Temp (C)'].iteritems():
    new = weather['Dew_P Temp (C)'][index]
    old = weather['Dew_P Temp (C)'][0]
    pct_diff = (new-old)/old*100
    weather['pct_diff'] = pct_diff

I think the problem is the weather['pct_diff'], it doesn't take the new it takes the last value of the data frame and subtracts it from old

So its always (2.1-3.9)/3.9*100 thus my percent change is always -46%.

The end result I want is this:

Date/Time        Temp (C)   Dew_P Temp (C)  Rel Hum (%)   pct_diff
0   2012-01-01   -1.8       -3.9             86           0.00%
1   2012-01-01   -1.8       -3.7             87           5.12%
2   2012-01-01   -1.8       -3.4             89           12.82%  

Any ideas? Thanks!

like image 491
dyao Avatar asked Sep 13 '16 20:09

dyao


People also ask

How to calculate percentage change in pandas pandas?

Pandas pct_change () method is applied on series with numeric data to calculate Percentage change after n number of elements. By default, it calculates percentage change of current element from the previous element. (Current-Previous/Previous) * 100. First, n (n=period) values are always NaN, since there is no previous value to calculate change.

What is % change in pandas Dataframe?

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.

What is PCT_change () method in Python?

Python | Pandas Series.pct_change () Last Updated : 24 Dec, 2018 Pandas pct_change () method is applied on series with numeric data to calculate Percentage change after n number of elements. By default, it calculates percentage change of current element from the previous element.

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.


1 Answers

You can use iat to access the scalar value (e.g. iat[0] accesses the first value in the series).

df = weather
df['pct_diff'] = df['Dew_P Temp (C)'] / df['Dew_P Temp (C)'].iat[0] - 1
like image 178
Alexander Avatar answered Sep 30 '22 19:09

Alexander