2
I have a csv file similar to this, with all the information of all the municipalities of Brazil (I shortened the csv to not get too extensive):
ESTADO,MUNICIPIO,HABITANTES,AREA
AC,ACRELÂNDIA,12538,1807.92
AC,ASSIS BRASIL,6072,4974.18
AC,BRASILÉIA,21398,3916.5
AC,BUJARI,8471,3034.87
AL,BATALHA,17076,320.92
AL,BELÉM,4551,48.63
AM,BARCELOS,25718,122476.12
AM,BARREIRINHA,27355,5750.57
AM,BENJAMIN CONSTANT,33411,8793.42
I am trying to add up only the number of inhabitants of the northern region, in this case, AC and AM. For this I used the code below (in Python 3.6.5):
import csv
populacao = 0
arquivo = open('brasil.csv', encoding='utf8')
for registro in csv.reader(arquivo):
habitantes = registro[2]
estado = registro[0]
if habitantes != 'habitantes':
if estado != 'estado':
regiao_norte = ['AC', 'AM']
for estado in regiao_norte:
populacao += int(habitantes)
print(populacao)
I get as sum: 381511598. But the sum is clearly incorrect. I thought using the list would act as a selector for the states I wanted to add. I can’t figure out what I’m missing. How can I make that sum correctly?
Thanks for the tips, @Miguel , worked out.
– Alineat
You’re welcome @Alineat, good study
– Miguel
In Pandas you could use the operator
in
? Something likedf.loc[df['ESTADO'] in ['AC', 'AM']]
– Woss
Hello @Andersoncarloswoss, dsculpa but just saw the question now. Unfortunately not,
ValueError: The truth value of a Series is ambiguous
, https://repl.it/repls/BleakIllegalArchitects. https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o– Miguel