这是我的代码:
def get_data(file_name):
data = pd.read_csv(file_name, error_bad_lines=False, encoding='latin-1')
X_parameter = []
Y_parameter = []
for single_square_feet, single_price_value in zip(data['Square_Feet'], data['Price']):
X_parameter.append([float(single_square_feet)])
Y_parameter.append(float(single_price_value))
return X_parameter, Y_parameter
X, Y = get_data('F:\Demo\input_data.csv')
print(X)
print(Y)
错误:
File "pandas\_libs\index.pyx", line 140, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\index.pyx", line 162, in pandas._libs.index.IndexEngine.get_loc
File "pandas\_libs\hashtable_class_helper.pxi", line 1492, in pandas._libs.hashtable.PyObjectHashTable.get_item
File "pandas\_libs\hashtable_class_helper.pxi", line 1500, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'Square_Feet'
我的.csv是:
答案 0 :(得分:0)
您在data['Square_feet']
中有一个键错误,这意味着您作为键输入的内容与任何列名都不匹配。我假设您有一些空格导致此。因此,为了解决此问题,请在您的 pd.read.csv
sep='\s*,\s*'
data = pd.read_csv(file_name, sep='\s*,\s*', error_bad_lines=False, encoding='latin-1')