问题:熊猫读表不带标题

我如何使用熊猫读取.csv文件(无标题),并且只希望使用列的子集(例如总共20列中的第4和第7列)?我似乎无法做usecols

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


回答 0

为了读取没有标题的csv,仅对于某些列,您需要传递params header=None以及usecols=[3,6]第4列和第7列:

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

查看文件

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

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

See the docs


回答 1

先前的答案是正确的,但我认为,额外的names参数可以使其完美,这是推荐的方式,尤其是在csv没有时headers

用途usecolsnames参数

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

补充阅读

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

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

这样您就可以通过以下方式检索数据

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

代替

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

说明

基于read_csv,当names显式传递时,header将表现为None而不是0,因此存在header=None时可以跳过names

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

Solution

Use usecols and names parameters

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

Additional reading

or use header=None to explicitly tells people that the csv has no headers (anyway both lines are identical)

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 names are passed explicitly, then header will be behaving like None instead of 0, so one can skip header=None when names exist.


回答 2

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

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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。