KeyError when indexing Pandas dataframe

后端 未结 2 455
庸人自扰
庸人自扰 2020-12-05 14:03

I am trying to read data from a csv file into a pandas dataframe, and access the first column \'Date\'

import pandas as pd
df_ticks=pd.read_csv(\'values.csv\         


        
相关标签:
2条回答
  • 2020-12-05 14:34

    As mentioned by alko, it is probably extra character at the beginning of your file. When using read_csv, you can specify encoding to deal with encoding and heading character, known as BOM (Byte order mark)

    df = pd.read_csv('values.csv', delimiter=',', encoding="utf-8-sig")
    

    This question finds some echoes on Stackoverflow: Pandas seems to ignore first column name when reading tab-delimited data, gives KeyError

    0 讨论(0)
  • 2020-12-05 14:51

    You most likely have an extra character at the beginning of your file, that is prepended to your first column name, 'Date'. Simply Copy / Paste your output to a non-unicode console produces.

    Index([u'?Date', u'Open', u'High', u'Low', u'Close', u'Volume'], dtype='object')
    
    0 讨论(0)
提交回复
热议问题