user6416338
user6416338

Reputation:

Python Pandas read_csv reads only single line from CSV file

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?

Upvotes: 0

Views: 6196

Answers (2)

Merlin
Merlin

Reputation: 25629

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

Upvotes: 0

MaThMaX
MaThMaX

Reputation: 2015

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

Upvotes: 2

Related Questions