Sum of column totals

0

I own this dataset: I would like to create command to sum the values of each year, and put in a list.

  lista=[]
  for i in range(2008, 2041):
   area[i].sum()
    lista.append(i)

More did not work the way I did, I would like some help.

    
asked by anonymous 09.08.2018 / 13:57

2 answers

0

I assumed you were using pandas.

Anyway, starting from this:

import pandas as pd

d = {'Distritos': ['Local1', 'Local2'], '2017': [30, 40], '2018': [50, 60]}
df = pd.DataFrame(data=d)
>>> print df
   2017  2018 Distritos
0    30    50    Local1
1    40    60    Local2

I take years like this:

anos = list(df.columns.values)
anos.remove('Distritos')
>>> print anos
['2017', '2018']

Add all of the same year:

lista = []
for ano in anos:
    lista.append(df[ano].sum())
>>> print lista
[70, 110]

Add all values from each region:

lista = []
for i in range(len(df)): #para cada região
    soma = 0
    for ano in anos: #seleciono os anos
        soma += df.iloc[i][ano]
    lista.append(soma)
>>> print lista
[80, 100]
    
09.08.2018 / 14:10
0

first try using for as long as a given exists.

lista = []
for i in arq:
    arq.split('?') #o '?' é o que separa cada índice no seu arquivo ou variável que contém a informação que quer

This way you have rows and columns now and the next step is just to see which range of index you want to add

i = 2
sum = 0
for i in range(n): #n tem que ser o numero de colunas que quer percorrer
 sum = sum + arq[i] 
lista.append(sum)

the complete code is:

lista = []
for i in arq:
    arq.split('?')
    sum = 0
    for j in range(2,n): #n tem que ser o numero de colunas que quer percorrer
        sum = sum + arq[j] 
    lista.append(sum)
    
09.08.2018 / 16:30