Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use read_csv to read only specific lines?

Tags:

python

pandas

csv

I have a csv file that looks like this:

TEST  
2012-05-01 00:00:00.203 ON 1  
2012-05-01 00:00:11.203 OFF 0  
2012-05-01 00:00:22.203 ON 1  
2012-05-01 00:00:33.203 OFF 0  
2012-05-01 00:00:44.203 OFF 0  
TEST  
2012-05-02 00:00:00.203 OFF 0  
2012-05-02 00:00:11.203 OFF 0  
2012-05-02 00:00:22.203 OFF 0  
2012-05-02 00:00:33.203 OFF 0  
2012-05-02 00:00:44.203 ON 1  
2012-05-02 00:00:55.203 OFF 0  

and cannot get rid of the "TEST" string.

Is it possible to check whether a line starts with a date and read only those that do?

like image 571
user1412286 Avatar asked May 23 '12 09:05

user1412286


People also ask

How do I read a specific line from a CSV file in pandas?

line_number = 8 # the row you want. 0-indexed import pandas as pd import sys # or `import itertools` import csv # you can wrap this block in a function: # (filename, line_number[, max_rows]) -> row with open(filename, 'r') as f: r = csv. reader(f) for i in range(sys.

How do I read specific rows in pandas?

In the Pandas DataFrame we can find the specified row value with the using function iloc(). In this function we pass the row number as parameter.


1 Answers

from cStringIO import StringIO
import pandas

s = StringIO()
with open('file.csv') as f:
    for line in f:
        if not line.startswith('TEST'):
            s.write(line)
s.seek(0) # "rewind" to the beginning of the StringIO object

pandas.read_csv(s) # with further parameters…
like image 145
eumiro Avatar answered Oct 03 '22 00:10

eumiro