Skip to content
Advertisement

Series Dataframe to List and Keeping Dates

This code outputs dates along with values as a series. I wanted to manipulate the values but I ended up losing the dates.

rgdp = fred.get_series('GDPC1')
fgdp=rgdp
rlistgdp=[] 
for a in range(len(rgdp)):
    rgdp2=((rgdp.iloc[a]/rgdp.iloc[a-1])**4-1) * 100
    rlistgdp.append(rgdp2)
rlistgdp

Series Dataframe:

Series to List:

How can I keep the dates along with the new values?

Advertisement

Answer

series = pd.Series([100,150,200,300], index=['a','b','c','d'])
series

a    100
b    150
c    200
d    300
dtype: int64
series2 = series.copy()
for i in range(1, len(series)):
    series2[i] = ((series[i]/series[i-1])**4-1) * 100
series2

a    100
b    406
c    216
d    406
dtype: int64

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.

Advertisement