Python 使用pd.read\u csv时删除标题时遇到问题

Python 使用pd.read\u csv时删除标题时遇到问题,python,python-2.7,pandas,Python,Python 2.7,Pandas,我有一个包含列标题的.csv,显示在下面。当我将文件作为数据帧接收时,需要抑制列标签 date,color,id,zip,weight,height,locale 11/25/2013,Blue,122468,1417464,3546600,254,7 当我发出以下命令时: df = pd.read_csv('c:/temp1/test_csv.csv', usecols=[4,5], names = ["zip","weight"], header = 0, nrows=10) 我得到:

我有一个包含列标题的.csv,显示在下面。当我将文件作为数据帧接收时,需要抑制列标签

date,color,id,zip,weight,height,locale
11/25/2013,Blue,122468,1417464,3546600,254,7
当我发出以下命令时:

 df = pd.read_csv('c:/temp1/test_csv.csv', usecols=[4,5], names = ["zip","weight"], header = 0, nrows=10)
我得到:

zip               weight
0   1417464       3546600
我尝试了header=True和header=0的各种操作。如果我不使用header=0,那么列将全部打印在行的顶部,如下所示:

    zip           weight
    height        locale
0   1417464       3546600
我尝试了skiprows=0和1,但都没有删除标题。但是,该命令会跳过指定的行

我真的需要一些额外的见解或解决方案。提前感谢您提供的任何帮助


Tiberius

我不确定我是否完全理解您为什么要删除标题,但只要没有任何其他以
'd'
开头的行,您可以按如下方式注释标题行:

>>> df = pd.read_csv('test.csv', usecols=[3,4], header=None, comment='d')  # comments out lines beginning with 'date,color' . . .
>>> df
         3        4
0  1417464  3546600
最好用交叉线字符(
#
)注释掉csv文件中的行,然后使用相同的方法(同样,只要没有注释掉任何其他带有交叉线的行):

我认为你是对的

因此,您可以将列名更改为
a
b

import pandas as pd
import numpy as np
import io

temp=u"""date,color,id,zip,weight,height,locale
11/25/2013,Blue,122468,1417464,3546600,254,7"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), usecols=[4,5], names = ["a","b"], header = 0 , nrows=10)
print df
         a    b
0  3546600  254
现在这些列有了新名称,而不是
weight
height

df = pd.read_csv(io.StringIO(temp), usecols=[4,5], header = 0 , nrows=10)
print df
    weight  height
0  3546600     254
您可以查看文档(我用粗体):

标题:整数,整数列表,默认为“推断”

要用作列名的行号,以及数据的开头。如果未传递名称,则默认为0,否则为无显式传递header=0以替换现有名称。header可以是整数列表,指定列上多索引的行位置,例如[0,1,3]。将跳过未指定的中间行(例如,本例中跳过2行)。请注意,如果skip_blank_lines=True,此参数将忽略注释行和空行,因此header=0表示数据的第一行,而不是文件的第一行


以@jezrael为例,如果要跳过标题并抑制反列标签,请执行以下操作:

import pandas as pd
import numpy as np
import io

temp=u"""date,color,id,zip,weight,height,locale
11/25/2013,Blue,122468,1417464,3546600,254,7"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), usecols=[4,5], header=None, skiprows=1)
print df
         4    5
0  3546600  254
import pandas as pd
import numpy as np
import io

temp=u"""date,color,id,zip,weight,height,locale
11/25/2013,Blue,122468,1417464,3546600,254,7"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), usecols=[4,5], header=None, skiprows=1)
print df
         4    5
0  3546600  254