Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.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 从csv中选择列_Python_Csv_Pandas - Fatal编程技术网

Python 从csv中选择列

Python 从csv中选择列,python,csv,pandas,Python,Csv,Pandas,我有一个使用熊猫的简单函数: def get_data(filename): '''function to read the data form the input csv file to use in the analysis''' with open(filename, 'r') as f: reader = pd.read_csv(f, sep=',', usecols=('candidate',' final track' ,' status'))

我有一个使用熊猫的简单函数:

def get_data(filename):
    '''function to read the data form the input csv file to use in the analysis'''
    with open(filename, 'r') as f:
        reader = pd.read_csv(f, sep=',', usecols=('candidate',' final track' ,' status'))                      

        return reader
然而,当我打印reader时,我会得到我指定的列右边的两列

为什么会这样?我怎样才能纠正它呢? 我刚接触熊猫,所以很抱歉犯了明显的错误

编辑:

我的csv文件的第一行是:


候选、超前-滞后、强度、宽度、edgetrack、track1、track2、edge track count、track1 count、track2 count、final track、status、track 1 available、track 2 available、edge与track 1相同、edge与track 2相同、valid track

csv文件第一行的格式是什么?另外,若您传递一个col列表,比如
usecols=['candidate','final track','status']
,会发生什么情况。我想我们之前在前一个问题中讨论过,您说使用绝对整数值有效,例如
1,10,11
,但列名无效。你用的是什么版本的熊猫?嗨,埃德,是的,我们用了,这个问题有点不同,所以我写了另一个。第一行=候选超前-滞后强度宽度边缘轨道轨道1轨道2边缘轨道计数轨道1计数轨道2计数最终轨道状态轨道1可用轨道2可用边缘与轨道1边缘相同与轨道2有效轨道相同。方括号给出了我的dsame答案。@AshleighClayton如果从
最终曲目
中删除空格,使其成为
最终曲目
,会发生什么?也可以用您自己的
name=full\u list\u col\u name
覆盖列名,也可以传递
usecols=cols\u of\u interest
,但这对我来说似乎太过分了。你能试着重新命名cols以替换空格吗underscores@AshleighClayton我刚刚创建了一个临时csv文件,其列标题与您的相同,并使用了与您相同的
usecols
参数,这在Pandas 0.12中运行良好。你用的是什么版本?