Pandas KeyError: value not in index

后端 未结 4 952
伪装坚强ぢ
伪装坚强ぢ 2020-12-08 14:06

I have the following code,

df = pd.read_csv(CsvFileName)

p = df.pivot_table(index=[\'Hour\'], columns=\'DOW\', values=\'Changes\', aggfunc=np.mean).round(0         


        
4条回答
  •  轮回少年
    2020-12-08 14:59

    I had a very similar issue. I got the same error because the csv contained spaces in the header. My csv contained a header "Gender " and I had it listed as:

    [['Gender']]
    

    If it's easy enough for you to access your csv, you can use the excel formula trim() to clip any spaces of the cells.

    or remove it like this

    df.columns = df.columns.to_series().apply(lambda x: x.strip())

提交回复
热议问题