询问用户在pd.read_csv()中读取哪一列

时间:2018-03-30 16:10:05

标签: python pandas

我有一个包含多列的大型数据集,我希望用户告诉我要分析哪个col。 到目前为止我有:

file = some_file
col_name = raw_input("Enter column name: ")

cols_used = ["X",col_name]
read_cols = pd.read_csv(file, usecols = cols_used, skiprows = [0,1], name =cols_used)
test = pd.unique(read_cols["X"])

由于某种原因,我没有拉正确的cols。当我硬编码col名称时,一切正常。我不知道还有什么可以尝试的。

1 个答案:

答案 0 :(得分:0)

这应该有效

file = some_file
col_name = raw_input("Enter column name: ")

cols_used = ["X",col_name]
read_cols = pd.read_csv(file, usecols = cols_used, name =cols_used)
test = pd.unique(read_cols["X"])

它接缝

skiprows = [0,1]

引起问题所以可能0会导致跳过列名....所以试试这个:

read_cols = pd.read_csv(file, usecols = cols_used, skiprows = [1,2], name =cols_used)