Data analysis with python list

Asked

Viewed 326 times

1

I have a txt list with the following data:

txt list.

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,CODIGO, POPULACAO,PIB,PIBPERCAPITA

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

  • View the data in a specific UF
  • show the data of some specified Ufs
  • Or show statistics as (average, minimum and maximum value)

I just don’t know where you’re going, how to read a column. I don’t know what the python function would be. If anyone can give me a path it’s already very helpful.

I have my code this way:

#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 comes out all messy. I got this code here on the site.

2 answers

2

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

pip install pandas

then import:

import pandas as pd

to read the file, use the function read_csv:

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

if you give a print 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 on repl.it

Reference:

  • 1

    What a dough! Much simpler and more organized!

  • Ahhhh! Very show! I will play here. With this answer I believe I can do the third question that also asks pandas as reference. Thank you @Noobsaibot.

  • If this answer solved your problem and there is no doubt left, mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you prefer the other answer, you can mark it as correct/accepted, but only one response can be marked that way. If you still have any questions or would like further clarification, feel free to comment.

0

I made a simple function that reads the file and returns a list with the data of the desired state. From there you can make calculations, static, 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 separates each data from a state into a list called state, then checks if there is any state as the parameter is entered then returns that list with the state data.

  • While I didn’t have a way to solve my question I was printing the results by column, but in a very wrong and crude way, not the way I wanted kkkk Follow the print as far as I could do: http://prntscr.com/jy2muq

  • Philip, take away a doubt... Here arquivo = open(diretorio_txt, 'r') in diretorio_txtI put the path of my txt file right there? And in def retornar_informacoes_estado(diretorio_txt, uf): I leave it the way it is? I tried to put in arquivo = open("c:/trabalho/questao2/lista.txt", "r") and tried to print out all the data and could not.

  • That puts the directory of your file! In the function you place the address and the state you want to have the return!

  • I’m sorry, Filipe, I... I’m beginner in python, these functions I’m not very good at making them and nor do I have a well developed programming logic... I can print from a def a simple sum, as in this image: http://prntscr.com/jy7ydb. But I’m not being able to print in this code, I can’t find the variable or fit the print to print the table on the console... If I can print the table on the console the rest I can get good, because it’s only conditions. That’s what I know more kkkk. Give me a help on that part?

  • In this part "data_file = data_file.split(' n')" you have all your values in matrices as follows [ [Uf, codigo, populacao, pib, pibpercapita], [Uf, codigo, populacao, pib, pibpercapita], [Uf, codigo, populacao, pib, pibpercapita]]. You will have each state in a position of that matrix

  • Later I will edit the answer and I will try to explain better every step I have made!

Show 2 more comments

Browser other questions tagged

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