1

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
JamieC113a
  • 11
  • 1
  • 7
  • What do you mean by "keep the dates"? A list of tuples: `[(date, value),...]`? a new series? Please show an example of the expected output. – Ignatius Reilly Jun 25 '22 at 19:02
  • I want it to look like the dates from "Series Dataframe", but change the values to "Series to List" – JamieC113a Jun 25 '22 at 19:05
  • check this one: https://stackoverflow.com/a/34856727/15032126 – Ignatius Reilly Jun 25 '22 at 19:11
  • Also, you may want to use `fgdp=rgdp.copy()`, otherwise rgdp is going to be modified when you modify fgdp. – Ignatius Reilly Jun 25 '22 at 19:16
  • Avoid using images, except when it's difficult to convey meaning through text. _Write_ examples of the data (not necessary the real data) and the expected output. And write your input data as code, so other people can start just by running it: [mre] and https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Ignatius Reilly Jun 25 '22 at 19:33

1 Answers1

0
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.

Ignatius Reilly
  • 1,594
  • 2
  • 6
  • 15