如果我使用此代码读取 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中没有包含索引列名“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/