Python KeyError:“列名称”

Python KeyError:“列名称”,python,numpy,keyerror,Python,Numpy,Keyerror,我正在编写一个python代码,它应该读取列的值,但是我得到了keyrerror:'column_name'错误。谁能告诉我如何解决这个问题 import numpy as np from sklearn.cluster import KMeans import pandas as pd ### For the purposes of this example, we store feature data from our ### dataframe `df`, in the `f1` an

我正在编写一个python代码,它应该读取列的值,但是我得到了keyrerror:'column_name'错误。谁能告诉我如何解决这个问题

import numpy as np
from sklearn.cluster import KMeans
import pandas as pd


### For the purposes of this example, we store feature data from our
### dataframe `df`, in the `f1` and `f2` arrays. We combine this into
### a feature matrix `X` before entering it into the algorithm.

df = pd.read_csv(r'C:\Users\Desktop\data.csv')

print (df)

#df = pd.read_csv(csv_file)

"""
saved_column = df.Distance_Feature
saved_column = df.Speeding_Feature

print(saved_column)
"""

f1 = df['Distance_Feature'].tolist()
f2 = df['Speeding_Feature'].tolist()

print(f1)
print(f2)

X=np.matrix(zip(f1,f2))

print(X)

kmeans = KMeans(n_clusters=2).fit(X)

谁能帮帮我吗

Asumming“C:\Users\Desktop\data.csv”包含以下数据

Distance_Feature Speeding_Feature
1   2
3   4
5   6
 ...
改变

df = pd.read_csv(r'C:\Users\Desktop\data.csv')


您可以发布错误消息吗?KeyError:“距离功能”它不是从Excel中读取列名和值。您可以在读取csv文件时传递列名。pd.read\u csvr'C:\Users\Desktop\data.csv',name=columns\u list
    df = pd.read_csv("data.txt",names=["Distance_Feature","Speeding_Feature"],sep= "\s+|\t+|\s+\t+|\t+\s+",header=1) 
    # Here it is assumed white space separator, if another separator is used change `sep`.