pandas read_csv index_col=None不使用每行末尾的分隔符

2024-06-10 21:29:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在阅读“Python for Data Analysis”一书,在“示例:2012联邦选举委员会数据库”一节中将数据读取到数据帧时遇到了问题。问题是数据的其中一列总是被设置为索引列,即使index_col参数被设置为None

以下是数据的链接:http://www.fec.gov/disclosurep/PDownload.do

以下是加载代码(为了节省检查时间,我将nrows设置为10):

import pandas as pd
fec = pd.read_csv('P00000001-ALL.csv',nrows=10,index_col=None)

简而言之,我排除了数据列输出,但这是我的输出(请不要使用索引值):

In [20]: fec

Out[20]:
<class 'pandas.core.frame.DataFrame'>
Index: 10 entries, C00410118 to C00410118
Data columns:
...
dtypes: float64(4), int64(3), object(11)

这是这本书的输出(同样排除了数据列):

In [13]: fec = read_csv('P00000001-ALL.csv')
In [14]: fec
Out[14]:
<class 'pandas.core.frame.DataFrame'>
Int64Index: 1001731 entries, 0 to 1001730
...
dtypes: float64(1), int64(1), object(14)

我的输出中的索引值实际上是文件中的第一列数据,然后将所有剩余数据向左移动一。有人知道如何防止这列数据被列为索引吗?我希望索引只增加+1个整数

我对Python和熊猫还比较陌生,所以我为给您带来的不便表示歉意。谢谢


Tags: csv数据innonepandasreaddataindex
3条回答

Re:craigts的回答是,对于那些在索引col中使用False或None参数时遇到问题的人,例如在试图删除范围索引的情况下,可以使用整数来指定要用作索引的列。例如:

df = pd.read_csv('file.csv', index_col=0)

上面将第一列设置为索引(在我的“常见情况”中不添加范围索引)

更新

鉴于这个答案很受欢迎,我想我应该添加一些上下文/演示:

# Setting up the dummy data
In [1]: df = pd.DataFrame({"A":[1, 2, 3], "B":[4, 5, 6]})

In [2]: df
Out[2]:
   A  B
0  1  4
1  2  5
2  3  6

In [3]: df.to_csv('file.csv', index=None)
File[3]:
A  B
1  4
2  5
3  6

无索引或无/假读取都将导致范围索引:

In [4]: pd.read_csv('file.csv')
Out[4]:
   A  B
0  1  4
1  2  5
2  3  6

# Note that this is the default behavior, so the same as In [4]
In [5]: pd.read_csv('file.csv', index_col=None)
Out[5]:
   A  B
0  1  4
1  2  5
2  3  6

In [6]: pd.read_csv('file.csv', index_col=False)
Out[6]:
   A  B
0  1  4
1  2  5
2  3  6

但是,如果我们指定“A”(第0列)实际上是索引,我们可以避免使用范围索引:

In [7]: pd.read_csv('file.csv', index_col=0)
Out[7]:
   B
A
1  4
2  5
3  6

快速回答

如果每行末尾都有分隔符,请使用index_col=False而不是index_col=None关闭索引列推断并放弃最后一列

更多细节

查看数据后,每行末尾都有一个逗号。以及这段引文(自本帖创建之日起,文档已被编辑):

index_col: column number, column name, or list of column numbers/names, to use as the index (row labels) of the resulting DataFrame. By default, it will number the rows without using any column, unless there is one more data column than there are headers, in which case the first column is taken as the index.

fromthe documentation显示您有n个头和n+1个数据列,并将第一列作为索引


编辑2014年10月20日-更多信息

我发现another valuable entry特别是关于尾部限制器以及如何简单地忽略它们:

If a file has one more column of data than the number of column names, the first column will be used as the DataFrame’s row names: ...

Ordinarily, you can achieve this behavior using the index_col option.

There are some exception cases when a file has been prepared with delimiters at the end of each data line, confusing the parser. To explicitly disable the index column inference and discard the last column, pass index_col=False: ...

如果pandas将第一行视为标题,则可以使用header=none:

df = pd.read_csv ("csv-file.csv", header=None)

这样熊猫会像对待任何一排一样对待你的第一排

相关问题 更多 >