Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 读取表时出错_Python_Pandas_Dataframe - Fatal编程技术网

Python 读取表时出错

Python 读取表时出错,python,pandas,dataframe,Python,Pandas,Dataframe,我正在使用以下代码读取具有569行和125列的df: df = pd.read_csv('data', header = None, sep = '\s+') df.info()的输出是: <class 'pandas.core.frame.DataFrame'> Int64Index: 569 entries, 0 to 568 Columns: 125 entries, 0 to 124 dtypes: float64(123), int64(2) memory usage:

我正在使用以下代码读取具有
569行
125列
的df:

df = pd.read_csv('data', header = None, sep = '\s+')
df.info()
的输出是:

<class 'pandas.core.frame.DataFrame'>
Int64Index: 569 entries, 0 to 568
Columns: 125 entries, 0 to 124
dtypes: float64(123), int64(2)
memory usage: 560.1 KB
0             1    2        3          4         5
0      0  0.000000e+00  773  19.7205  25.944920  0.205061  
1      1  1.000000e+00  772  19.7205  25.944920  0.205061
2      2  2.100000e+00  748  19.7205  25.944920  0.205061
3      3  3.310000e+00  763  19.7205  25.944920  0.205061
...
568  568  1.010799e+10  697  19.7205  25.524932  0.199651
当我写入
打印df['X.1']
打印df['1']
时,我收到一个错误:

KeyError: 'X.1'

KeyError: '1'
有人能给我解释一下为什么我会出错吗


谢谢

IIUC您需要调用
df[1]
,因为您的列是整数。您可以使用索引的
dtype
调用
df.columns
来检查它。 如果您有类似的内容,则需要调用类似整数:

In [214]: df.columns
Out[214]: Int64Index([1, 2, 3, 4, 5], dtype='int64')

In [215]: df[1]
Out[215]: 
0    0.00
1    1.00
2    2.10
3    3.31
Name: 1, dtype: float64
如果列名为
str
,则需要调用
df['1']

In [216]: df.columns = map(str, range(1,6))

In [218]: df.columns
Out[218]: Index(['1', '2', '3', '4', '5'], dtype='object')

In [219]: df['1']
Out[219]: 
0    0.00
1    1.00
2    2.10
3    3.31
Name: 1, dtype: float64

IIUC您需要调用
df[1]
,因为您的列是整数。您可以使用索引的
dtype
调用
df.columns
来检查它。 如果您有类似的内容,则需要调用类似整数:

In [214]: df.columns
Out[214]: Int64Index([1, 2, 3, 4, 5], dtype='int64')

In [215]: df[1]
Out[215]: 
0    0.00
1    1.00
2    2.10
3    3.31
Name: 1, dtype: float64
如果列名为
str
,则需要调用
df['1']

In [216]: df.columns = map(str, range(1,6))

In [218]: df.columns
Out[218]: Index(['1', '2', '3', '4', '5'], dtype='object')

In [219]: df['1']
Out[219]: 
0    0.00
1    1.00
2    2.10
3    3.31
Name: 1, dtype: float64

检查您的列:
df.columns.tolist()
将向您展示您的列真正是什么检查您的列:
df.columns.tolist()
将向您展示您的列真正是什么这是一个愚蠢的错误!!谢谢@Anton提供的详细信息。真是个愚蠢的错误!!谢谢@Anton提供的详细信息。