简体   繁体   中英

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

我认为您有separator tab ,因此需要将read_csv默认sep=',' (id 无sep参数)更改为sep='\\t'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM