我试图用pandas读取txt文件中的值,并用matploitlib绘制散点图,但在尝试不同的方法时,我总是会遇到各种各样的错误
我的txt文件摘要。。。你知道吗
Brain Body
0.37 0.117182754
73 1.349981613
70 0.925010921
0.8 0.007620352
0.15 0.001406136
50 0.419981176
from pandas import*
import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
#Read_data
dataframe = pd.read_csv('./brain_body.txt' , header = None);
x_values = dataframe[['Brain']]
y_values = dataframe[['Body']]
#Training Model on data
body_reg = linear_model.LinearRegression()
body_reg.fit(x_values, y_values)
plt.scatter(x_values, y_values)
plt.plot(x_values, body_reg.predict(x_values))
plt.show()
用这个代码我得到了下面的错误信息
Traceback (most recent call last):
File "C:/Users/User/Documents/body_brain_prediction.py", line 9, in <module>
x_values = dataframe[['Brain']]
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\frame.py", line 2981, in __getitem__
indexer = self.loc._convert_to_indexer(key, axis=1, raise_missing=True)
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexing.py", line 1271, in _convert_to_indexer
return self._get_listlike_indexer(obj, axis, **kwargs)[1]
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexing.py", line 1078, in _get_listlike_indexer
keyarr, indexer, o._get_axis_number(axis), raise_missing=raise_missing
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\core\indexing.py", line 1163, in _validate_read_indexer
key=key, axis=self.obj._get_axis_name(axis)
KeyError: "None of [Index(['Brain'], dtype='object')] are in the [columns]"
如果将
read_csv
与header=None
一起使用,则熊猫将假定没有头。您应该设置header=0
,告诉熊猫第一行有标题。你知道吗docs有更多信息
请尝试以下代码,并通过放置制表符空格来格式化.txt文件
您错误地假设文件中的数据是逗号分隔的,并且没有标题。实际上,它的没有头,并且是空格分隔的。读取数据的正确方法如下:
相关问题 更多 >
编程相关推荐