python pandas :为什么我不能在同一个 read_csv 语句中同时使用 index_col 和 usecols?提升值错误

python pandas : Why can't I use both index_col and usecols in the same read_csv statement ? raised valueError

如果我使用此代码读取 csv 文件:

    df = pd.read_csv('amazon2.csv'
                 , names=["year","state","month","number","date"]
                 , index_col = ['month']
                 , usecols=["year","state","number"]
                 , encoding = "ISO-8859-1")

会引发 valueError:

raise ValueError("Index {col} invalid".format(col=col))

ValueError: Index month invalid

但如果 usecols 或 index_col 被注释掉则不会引发错误 提前致谢! 数据库看起来像这样:

错误来源是索引列名"month"没有包含在列列表中:usecols。

df1=pd.read_csv("test.csv",index_col="month",usecols=["year","state","number","date","month"])

输出:

          year  state  number      date
month                       
Janeiro   1998   Acre       0  1998/1/1
Janeiro1  1998   Acre       1  1998/1/1
Janeiro1  1999  Acre2       2  1999/1/1
Janeiro2  2000   Acre       3  2000/1/1
Janeiro2  2000  Acre1       4  2000/1/1

但我同意索引列中不应有重复值