PythonPandas-按键错误:“没有索引...在列中

5n0oy7gb  于 2023-02-06  发布在  Python
关注(0)|答案(1)|浏览(332)

尝试红色一个简单的csv文件,然后尝试可视化其中一个变量。但它向我显示了关键错误。

import csv
import pandas
from sklearn import linear_model

df = pandas.read_csv("C:\\Users\\kinga\\Desktop\\Testing\\FF.csv")

X = df[['D', 'S']]
y = df['HPC']

regr = linear_model.LinearRegression()
regr.fit(X, y)

predictedHumains = regr.predict([[24, 6]])

print(predictedHumains)

print(regr.coef_)

错误代码

File "c:\Users\kinga\Desktop\Testing\feux de foret.py", line 7, in <module>
    X = df[['D', 'S']]
  File "C:\Users\kinga\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\frame.py", line 3511, in __getitem__
    indexer = self.columns._get_indexer_strict(key, "columns")[1]
  File "C:\Users\kinga\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 5782, in _get_indexer_strict
    self._raise_if_missing(keyarr, indexer, axis_name)
  File "C:\Users\kinga\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\core\indexes\base.py", line 5842, in _raise_if_missing
    raise KeyError(f"None of [{key}] are in the [{axis_name}]")KeyError: "None of [Index(['D', 'S'], dtype='object')] are in the [columns]"
PS C:\Users\kinga>
daolsyd0

daolsyd01#

您的csv文件使用“;'作为列值的分隔符。因此,您需要传递sep =';'在Pandas阅读功能。建议由@约翰戈登。试试这个noe

df = pandas.read_csv("C:\\Users\\kinga\\Desktop\\Testing\\FF.csv", sep=';')

相关问题