我发现这种类型的问题已经被问了很多次,但我没有像我这样的简单案例:(
我有一个test.txt文件
1 2 3
4 5 6
7 8 9
我写了以下Python代码
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])
它会导致以下错误消息
raise IndexError("single positional indexer is out-of-bounds")
IndexError: single positional indexer is out-of-bounds
我想得到2 5 8
,哪里出了问题?
首先,您不需要import csv
,因为您不使用该模块。
其次,您的文件不是以逗号分隔的值。它是空间分隔的。你必须告诉读者这一点:
a0 = pd.read_csv(file_0, sep="s+", header = None)