2

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

1 Answer 1

2

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')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.