Read CSV file, Columns [duplicate]

1

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

Eg:

if(o conteudo do csv coluna 1=="positivo" && conteudo do csv coluna 2 =="negativo")
    
asked by anonymous 25.07.2017 / 20:04

1 answer

1

[TL; DR]
There are several ways to do this, an easy one would be to use the 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 through the dataframe rows:

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, no repl.it, or pandas it seems to freeze. : -)

    
25.07.2017 / 23:21