首页 > 解决方案 > Python:pd.read_csv(..., index_col='year') 后出现“标签不在列中”错误

问题描述

我正在尝试访问我的数据框中的列,但是它不起作用

import pandas as pd
df = pd.read_csv(data, index_col='year')
dfyear = df.loc[:,'year']

我收到以下错误:

KeyError: 'the label [year] is not in the [columns]'

我该如何解决?

标签: pythonpandasmatplotlib

解决方案


我认为year是你的索引。尝试dfyear = df.index代替dfyear = df.loc[:,'year']


推荐阅读