Add historical

Asked

Viewed 62 times

0

I have the dataset called Gd, taken from an xls, an example:

 DATA_CRIA   FROTA
 1971        2
 1972        19
 1973        19

This dataset has several entries with the pattern listed above, I would like to create a dictionary with the values of each years added with previous, for example:

{1971:2, 1972:21, 1973:40}

That’s why I did this:

dic_ano = {}
for ano in anos:
    dic_ano.update({ano:0})

for i in range(len(gd)):
    for ano in anos:
        if gd.iloc[i]['DATA_CRIA'] == ano:
            dic_ano[ano] = dic_ano[ano]+1

But the figures aren’t coming out with the right count, what should I do?

  • 1

    Is that what you want? https://repl.it/@marcelobonifazio/Rosybrownorchidmegabyte

  • I was wrong in my explanation, I implied that the dataset was only these values, but I already arranged.

  • I have to convert the data to a dictionary, but the data is not in a dictionary.

  • You happen to be using Pandas?

  • yes, but I haven’t been able to.

1 answer

0


#Pego os anos
anos = gd['DATA_CRIA'].tolist()
#OU anos = gd['DATA_CRIA'].values

#ordeno os anos por precaução
anos.sort()

dic_ano = {}
soma_frota = 0
for ano in anos:
    #filtro o gd onde DATA_CRIA é igual ao ano, pego o valor da coluna FROTA
    soma_frota += gd[gd['DATA_CRIA'] == ano]['FROTA'].values[0]
    dic_ano.update({ano:soma_frota})

>>> print(dic_ano)
{1971: 2, 1972: 21, 1973: 40}
  • It worked, but with a problem I forgot to mention, the years have to come out in a sequence I already have:anos=
[1980, 1991, 2000, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2025, 2030, 2040]

  • Ah, so just use your haha sequence

Browser other questions tagged

You are not signed in. Login or sign up in order to post.