This code outputs dates along with values as a series. I wanted to manipulate the values but I ended up losing the dates.
JavaScript
x
8
1
rgdp = fred.get_series('GDPC1')
2
fgdp=rgdp
3
rlistgdp=[]
4
for a in range(len(rgdp)):
5
rgdp2=((rgdp.iloc[a]/rgdp.iloc[a-1])**4-1) * 100
6
rlistgdp.append(rgdp2)
7
rlistgdp
8
How can I keep the dates along with the new values?
Advertisement
Answer
JavaScript
1
2
1
series = pd.Series([100,150,200,300], index=['a','b','c','d'])
2
JavaScript
1
8
1
series
2
3
a 100
4
b 150
5
c 200
6
d 300
7
dtype: int64
8
JavaScript
1
4
1
series2 = series.copy()
2
for i in range(1, len(series)):
3
series2[i] = ((series[i]/series[i-1])**4-1) * 100
4
JavaScript
1
8
1
series2
2
3
a 100
4
b 406
5
c 216
6
d 406
7
dtype: int64
8
Notice that I used copy()
so fgpd doesn’t get modified.
The range is set to start in 1 because I don’t know how you plan to modify the first value.