How to store the result of an iteration in a new variable using python?

1

Good morning everyone!

I'm parsing a conjunto de dados de compartilhamento de bicicletas . In this dataset there is a column named 'birthyear' , which indicates the year the user was born.

I'm trying to turn this column into a time series column. To do this, I created the following iteration:

I saved this column in a variable called yr

for i in yr:
    x = datetime(yr[i],1,1)
    print(x)'

The output is:

1964-01-01 00:00:00
1986-01-01 00:00:00
1967-01-01 00:00:00
1976-01-01 00:00:00
1991-01-01 00:00:00
1975-01-01 00:00:00
1975-01-01 00:00:00

But when I store the output in the 'x' variable, it does not store this list, but only the first line.

Saída: datetime.datetime(1975, 1, 1, 0, 0)

How can I solve this problem?

    
asked by anonymous 09.09.2018 / 15:35

1 answer

3

If I get it right, you can add elements to a python list using append

x=[]
for i in yr:
    x.append(datetime(yr[i],1,1))

print(x)

This will add all iterations of the loop in the x list, the way you demonstrated in your code, at each iteration the variable x is overwritten, at the end of the loop the variable x will only have stored the value of the last iteration.

    
09.09.2018 / 16:04