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

标签 python pandas 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 被注释掉,则不会引发错误 提前致谢! 数据库如下所示: amazon2.csv

最佳答案

错误来源是由于列列表:usecols中没有包含索引列名“month”引起的。

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

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

关于python pandas : Why can't I use both index_col and usecols in the same read_csv statement ? 引发 valueError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59148830/

相关文章:

python - 如何在不引用旧列名称且不创建新数据框的情况下更改列名称?

python - pandas groupby的分配结果

Django 3.0 值错误

javascript - Web Socket 未连接到 Tornado 服务器

python - pywinauto 的滚动问题

python-3.x - 从冗余二进制数据集创建多标签数据集以进行多标签分类

python - ValueError:检查目标时出错:预期 main_prediction 有 3 个维度,但得到形状为 (1128, 1) 的数组

python - 在Python turtle 中使用while循环控制游戏

python - Django CMS - 导航顺序

variables - Tensorflow 值错误 : Variable already exists, 不允许