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!
You can use iat
to access the scalar value (eg 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
IIUC you can do it this way:
In [88]: ((weather['Dew Point Temp (C)'] - weather.ix[0, 'Dew Point Temp (C)']).abs() / weather.ix[0, 'Dew Point Temp (C)']).abs() * 100
Out[88]:
0 0.000000
1 5.128205
2 12.820513
3 17.948718
4 15.384615
5 15.384615
6 20.512821
7 7.692308
8 7.692308
9 20.512821
I find this more graceful
weather['Dew_P Temp (C)'].pct_change().fillna(0).add(1).cumprod().sub(1)
0 0.000000
1 -0.051282
2 -0.128205
3 -0.179487
4 -0.153846
Name: Dew_P Temp (C), dtype: float64
To get your expected output with absolute values
weather['pct_diff'] = weather['Dew_P Temp (C)'].pct_change().fillna(0).add(1).cumprod().sub(1).abs()
weather
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.