Data analysis with list in python

1

I have a list in txt with the following data:

lista.txt

AC;12;733559;8476515;11.57
AL;27;3120494;24574808;7.87
AM;13;3483985;59779292;17.17
AP;16;669526;8265965;12.36
BA;29;14016906;154340458;11.01
CE;23;8452381;77865415;9.22
DF;53;2570160;149906319;58.49
ES;32;3514952;82121834;23.38
GO;52;6003788;97575930;16.25
MA;21;6574789;45255942;6.89
MG;31;19597330;351380905;17.93
MS;50;2449024;43514207;17.77
MT;51;3035122;59599990;19.64
PA;15;7581051;77847597;10.26
PB;25;3766528;31947059;8.48
PE;26;8796448;95186714;10.82
PI;22;3110292;22060161;7.07
PR;41;10444526;217289677;20.81
RJ;33;15989929;407122794;25.46
RN;24;3168027;32338895;10.21
RO;11;1562409;23560644;15.10
RR;14;450479;6340601;14.05
RS;43;10693929;252482597;23.61
SC;42;6248436;152482338;24.40
SE;28;2068017;23932155;11.57
SP;35;41262199;1247595927;30.24
TO;17;1383445;17240135;12.46

Where each column works, respectively: UF, CODE, POPULATION, GDP, PIBPERCAPITA

When reading this file, I need to have the program return a menu with functions like:

  • View the data a specific UF
  • show data for some specified UF
  • Or show statistics like (average, minimum value and maximum value)

But I do not know where it goes, like reading a column. I do not know what the function in python would be. If anyone can give me a way it's already a lot of help.

I have my code like this:

#enconding utf-8

import os
import platform

plataforma = platform.system()

if (plataforma == "Windows"):
    os.system("cls")
else:
    os.system("clear")

arquivo = open("C:\python\lista.txt", "r")
texto = arquivo.read()

dados = []
with open("C:/python/lista.txt") as lista:
    for linha in lista:
        if(linha.strip() != ''):
            coluna = [i.strip() for i in linha.split(' ')[:3:3]]
            dados.append(coluna)
print(dados)

I can print but it all comes out messy. I got this code here on the site.

    
asked by anonymous 22.06.2018 / 05:23

2 answers

2

You need the pandas module. To install run the command:

pip install pandas

then import:

import pandas as pd

To read the file, use the read_csv function. :

df = pd.read_csv('lista.txt', delimiter=";",
       names=["UF","CODIGO", "POPULACAO","PIB","PIBPERCAPITA"])

If you give a print it will have the following output:

    UF  CODIGO  POPULACAO         PIB  PIBPERCAPITA
0   AC      12     733559     8476515         11.57
1   AL      27    3120494    24574808          7.87
2   AM      13    3483985    59779292         17.17
3   AP      16     669526     8265965         12.36
4   BA      29   14016906   154340458         11.01
5   CE      23    8452381    77865415          9.22
6   DF      53    2570160   149906319         58.49
...

Complete code:

#enconding utf-8
import pandas as pd
df = pd.read_csv('lista.txt', delimiter=";",
       names=["UF","CODIGO", "POPULACAO","PIB","PIBPERCAPITA"])
print(df)

See working at repl.it

Reference:

22.06.2018 / 06:14
0

I made a simple function that reads the file and returns a list with the desired state data. From there you can do calculations, statistics, or whatever you want with the data.

def retornar_informacoes_estado(diretorio_txt, uf):

    try:
        arquivo = open(diretorio_txt, 'r')
        dados_arquivo = arquivo.read()
        arquivo.close()

        dados_arquivo = dados_arquivo.split('\n')

        tamanho_dados = len(dados_arquivo)

        for i in range(tamanho_dados):

            estado = dados_arquivo[i].split(';')

            if(estado[0] == uf):
                return estado

    except:
        return False

The function reads the file and separates each line into a list. Then it separates each data from a state into a list called state, then checks if there is any state according to the input of the parameter then returns that list with the state data.

    
22.06.2018 / 06:10