Read CSV file ,Columns

Asked

Viewed 4,275 times

1

I would like to know how to read a CSV file taking the first and second column and compare the string contained in it with another string I want in the code.

Ex.:

if(o conteudo do csv coluna 1=="positivo" && conteudo do csv coluna 2 =="negativo")
  • no !! I want to pick up the line [0]column [1] and so on

  • You want to scroll through all rows and all columns of CSV ?

  • Managed to implement my response?

  • yes ,thank you!!!

1 answer

2


[TL;DR]
There are several ways to do this, a very easy one would be using pandas:

import pandas as pd
import io

# Simulando um CSV
s = '''
Mesa,Entrada,Saida,Conta
01,16:00,18:00,95.00
02,14:00,18:00,195.00
03,18:00,21:00,75.00
04,16:30,18:30,75.00
05,16:00,18:45,178.00
'''

# Lendo o csv 
df = pd.read_csv(io.StringIO(s), usecols=['Mesa', 'Entrada', 'Saida', 'Conta'])

# Imprimindo o resultado
print(df)
       Mesa Entrada  Saida  Conta
0     1   16:00  18:00   95.0
1     2   14:00  18:00  195.0
2     3   18:00  21:00   75.0
3     4   16:30  18:30   75.0
4     5   16:00  18:45  178.0

# Imprimindo coluna específica
print(df['Entrada'])
0    16:00
1    14:00
2    18:00
3    16:30
4    16:00
Name: Entrada, dtype: object  

To iterate (browse) on the dataframe lines:

for index, row in df.iterrows():
    print (row['Entrada'], row['Conta'])

Exit:

16:00 95.0
14:00 195.0
18:00 75.0
16:30 75.0
16:00 178.0

See working on repl.it *

* Have patience, in repl.it, the pandas seem to freeze. :-)

Browser other questions tagged

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