简体   繁体   中英

IndexError in reading a csv file with pandas

I have a CSV file that I am trying to read through with the pandas module in python..(i am coding in Ubuntu).

pd.read_csv("filename.csv",skiprows=2)

I want to skip two rows so I am using skiprows argument here. (The file named filename.csv is created through a C program.) The problem which I am facing is that when I try to read the file directly then I am getting an IndexError as follows:

IndexError: index 0 is out of bounds for axis 0 with size 0

and one warning as

FutureWarning: elementwise comparison failed; returning scalar instead, but in the future will perform elementwise comparison
res_values = method(rvalues)

But at the same time if I open my CSV file and just save it without doing any editing in it..my error gets resolved. So for every time, I need to read my CSV file I need to open it once through Libre Office(in Ubuntu) and save that file using ctrl+s and further in the option save as CSV text. Then, my program read it without any errors. But on not saving, it produces the above-mentioned error. Please help me with this issue. Thanks in Advance.

I think it is about the format of csv file when exporting. Because of after open csv and save it, it gets the correct format. Check the file with notepad++ to correct the delimiter of csv file.

This is an IndexError in python, which means that we're trying to access an index that isn't there. I wrote is a very simple example to understand this error. Here I try to assign any value to some index.

import numpy as np

arr = np.array([], dtype=np.int64) 
print(arr.shape)
arr[0] = 23   

在此处输入图像描述

It might be There were a few instances of empty data frames that were causing the error. If you have numeric and non-numeric data in your index column. Then numpy gets confused when it tries to check whether the index is ordered.

For Solution:

df = pd.read_csv('your_file.tsv', sep='\t', header=0) 
df.set_index(['0'], inplace=True)

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