简体   繁体   English

"ValueError:索引 DATE 无效,标题行上有 pandas.read_csv"

[英]ValueError: Index DATE invalid with pandas.read_csv on header row

Trying to create a dictionary with the key's being the first row of the csv file and the value's being a dictionary of {first column: corresponding column to row}:尝试创建一个字典,键是 csv 文件的第一行,值是 {第一列:对应列到行}的字典:

import pandas as pd

df = pd.read_csv('~/StockMachine/data_stocks.csv', index_col=['DATE'], sep=',\s+')

data = df.to_dict()

print(data)

Similiar thing happened to me and in my case some readings of ['DATE'] were strings with empty spaces inside.类似的事情发生在我身上,在我的情况下, ['DATE']一些读数是内部有空格的字符串。 Maybe if you would do something like:也许如果你会做这样的事情:

import pandas as pd

df = pd.read_csv('~/StockMachine/data_stocks.csv', sep=',\s+')

df['DATE'] = df['DATE'].apply(lambda x: str(x.strip())).astype(str)

df.set_index('DATE', inplace=True)

print(df.head())

我有同样的问题然后意识到 Col_Index 的选定列不是我的脚本中指定的选定 Header=1 行的一部分

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM