0

I am writing a code to read and extract parameters from each row of a CSV file.

From each row, I get a list of parameter values arranged by datestamps and values. An example csv file is as below:

Row1: 'Fanspeed','Value=32','Datetime=05-01-2015','Fanspeed','Value=32','Datetime=05-02-2015' 

Row2: 'Fanspeed','Value=32','Datetime=05-03-2015','Fanspeed','Value=32','Datetime=05-04-2015'

If I use the Pandas read_csv to read in the file and then print the output, it only prints the first row. However, when I use the csv.reader function, I get the correct output. My program looks like this:

csv_f = pd.read_csv('test.csv')
for row in csv_f: 
    csv_f = pd.read_csv('test.csv') 
    print csv_f

I get only the following output:

'Fanspeed','Value=32','Datetime=05-01-2015','Fanspeed','Value=32','Datetime=05-02-2015'

However, on running the same program with the csv.reader function as below:

f = open('test.csv') 
csv_f = csv.reader(f)
for row in csv_f: 
    csv_f = pd.read_csv('test.csv') 
    print csv_f

I get the correct output. Could someone help me?

3
  • They're semantically different, in the first one you open a file object and reuse it so it reads a line at a time from the same file object, in the second code snippet you're iterating over each line in the csv and then you re-open the csv again which resets back to the first line Commented Jun 2, 2016 at 20:10
  • 1
    pandas.read_csv() reads the whole file and create a dataframe with its content, that's like the first thing told in the doc. But none of your code versions actually makes sense anyway, why do you rebind the thing you iterate on ? Why mix csv module and pandas ? And your example file is not even correct csv. Commented Jun 2, 2016 at 20:23
  • How many rows does the csv file have in it? Commented Jun 3, 2016 at 3:29

2 Answers 2

2

First of all , you can try to pass 'header=None' to indicate the first row will be data instead of the headers.

csv_f = pd.read_csv('test.csv', header=None) 
print csv_f

Secondly, what the read_csc returns is already a DataFrame, so you should directly working on it. More information pandas.read_csv

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

    csv_f = pd.read_csv('test.csv')
    print csv_f.head()
    #for row in csv_f: 
        #csv_f = pd.read_csv('test.csv') 
        #print csv_f

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.