Dhara
Dhara

Reputation: 312

Python read 3 columns in a csv file

I want to read columns from a .csv file.

Here's the code which I tried:

inputFileName = "some.csv"
dataset = pd.read_csv(inputFileName)
dataset = np.array(dataset)
ihr = np.array(dataset[:])
x = dataset[:, 0]
y = dataset[:,1]

The intention of the code is to read first column values and second column values from some.csv and store it as x and y respectively. But, it fails to do so.

When I execute this, this is what I get:

['2.070\t72.2892\t0' '2.900\t72.2892\t0' '3.730\t68.1818\t0' ...,
 '29562.020\t74.0741\t0' '29562.830\t75\t0' '29563.630\t73.1707\t0']

Traceback (most recent call last):
  File "/home/ubuntu/Desktop/major_thesis/ECG/transpose_ihr.py", line 32, in <module>
    y = dataset[:, 1]
IndexError: index 1 is out of bounds for axis 1 with size 1

The problem with this code is it reads the whole data file and store it as dataset. So what should I do if I want to read those 2 columns individually

Thanks in advance

Upvotes: 2

Views: 810

Answers (1)

jezrael
jezrael

Reputation: 862511

I think you have separator tab, so need change default sep=',' (id no sep parameter) to sep='\t' in read_csv:

dataset = pd.read_csv(inputFileName, sep='\t')

Upvotes: 2

Related Questions