AlphaF20
AlphaF20

Reputation: 603

"IndexError: single positional indexer is out-of-bounds" in reading file

I found this type of question has been asked a lot of times, but I have not got a simple case like me :(

I have a test.txt file

1 2 3
4 5 6 
7 8 9

I wrote the following Python code

import pandas as pd
import csv
file_0 = 'test.txt'
a0 = pd.read_csv(file_0, header = None)
#a0_df = pd.DataFrame(a0)
print(a0)
print(a0.iloc[:,1])

It lead to the following error message

    raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds

I want to get 2 5 8, where things go wrong?

Upvotes: 1

Views: 478

Answers (1)

DYZ
DYZ

Reputation: 57085

First, you do not need to import csv because you do not use that module.

Second, your file is not a comma-separated values. It is space-separated. You have to tell the reader about that:

a0 = pd.read_csv(file_0, sep="\s+", header = None)

Upvotes: 2

Related Questions