KeyError:' column_name'

时间:2018-02-06 10:23:54

标签: python numpy keyerror

我正在编写一个python代码,它应该读取列的值,但我得到的是KeyError:' 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)

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:0)

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')

    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`.