Python 熊猫在没有标题的表中读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29287224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:19:43  来源:igfitidea点击:

Pandas read in table without headers

pythonpandas

提问by user308827

How can I read in a .csv file (with no headers) and when I only want a subset of the columns (say 4th and 7th out of a total of 20 columns), using pandas? I cannot seem to be able to do usecols

当我只想要列的子集(比如总共 20 列中的第 4 和第 7 列)时,如何使用 Pandas 读取 .csv 文件(没有标题)?我似乎无法做到usecols

采纳答案by EdChum

In order to read a csv in that doesn't have a header and for only certain columns you need to pass params header=Noneand usecols=[3,6]for the 4th and 7th columns:

为了读取没有标题的 csv,并且只有某些列需要传递参数header=None以及usecols=[3,6]第 4 列和第 7 列:

df = pd.read_csv(file_path, header=None, usecols=[3,6])

See the docs

查看文档

回答by Alex

Make sure you specify pass header=Noneand add usecols=[3,6]for the 4th and 7th columns.

确保为第 4 列和第 7 列指定 passheader=None和 add usecols=[3,6]

回答by ch33hau

Previous answers were good and correct, but in my opinion, an extra namesparameter will make it perfect, and it should be the recommended way, especially when the csv has no headers.

以前的答案是好的和正确的,但在我看来,一个额外的names参数会使它变得完美,它应该是推荐的方式,尤其是当 csv 没有headers.

Solution

解决方案

Use usecolsand namesparameters

用途usecolsnames参数

df = pd.read_csv(file_path, usecols=[3,6], names=['colA', 'colB'])

Additional reading

补充阅读

or use header=Noneto explicitly tells people that the csvhas no headers (anyway both lines are identical)

或用于header=None明确告诉人们csv没有标题(无论如何两行都是相同的

df = pd.read_csv(file_path, usecols=[3,6], names=['colA', 'colB'], header=None)

So that you can retrieve your data by

以便您可以通过以下方式检索数据

# with `names` parameter
df['colA']
df['colB'] 

instead of

代替

# without `names` parameter
df[0]
df[1]

Explain

解释

Based on read_csv, when namesare passed explicitly, then headerwill be behaving like Noneinstead of 0, so one can skip header=Nonewhen namesexist.

基于read_csv,当names被显式传递时,则header行为类似于None而不是0,因此可以header=Nonenames存在时跳过。