나는 팬더를 처음 사용하고 파이썬 2.7에서 스 캐터 플롯을 만들려고했는데 .txt 파일에 데이터 세트가 있는데 (쉼표로 구분)팬더를 사용하여 파이썬에서 텍스트 파일을 읽는 방법
6.1101,17.592
5.5277,9.1302
8.5186,13.662
7.0032,11.854
5.8598,6.8233
8.3829,11.886
7.4764,4.3483
import pandas as pd
import matplotlib.pyplot as mplt
# Taking Dataset using Pandas
input_data = pd.read_csv('data.txt');
#input_data.head(5)
데이터 집합에 헤더가없는 분산 형 플롯에서 위의 데이터를 플로팅하는 방법은 무엇입니까?
자습서와 예제에서 데이터 세트에 열 머리글이있는 경우 분산 형 플롯을 플롯 할 수 있습니다. x와 y를 .txt 파일에있는 데이터 세트의 두 열에 대한 헤더로 넣고 시도해 보았습니다. 아래 코드를 시도했습니다.
Traceback (most recent call last):
File "E:\IIT Madras\Research\Experiments\Machine Learning\Linear Regression\Linear_Regression.py", line 16, in <module>
y_value = input_data[['y']]
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1791, in __getitem__
return self._getitem_array(key)
File "C:\Python27\lib\site-packages\pandas\core\frame.py", line 1835, in _getitem_array
indexer = self.ix._convert_to_indexer(key, axis=1)
File "C:\Python27\lib\site-packages\pandas\core\indexing.py", line 1112, in _convert_to_indexer
raise KeyError('%s not in index' % objarr[mask])
KeyError: "['y'] not in index"
아래와 같이
input_data = pd.read_csv('data.txt');
#input_data.head(5)
x_value = input_data[['x']]
y_value = input_data[['y']]
mplt.scatter(x_value, y_value)
하지만 여전히 나는이 (와 헤더 이름이없는)을 처리하는 더 좋은 방법이 있나요 오류를 받고 있어요?
편집 :
다음 Ishan을 거쳐 나를 위해 일한 응답
input_data = pd.read_csv('data.txt', header =None);
x_value = input_data[[0]]
y_value = input_data[[1]]
mplt.scatter(x_value, y_value)
mplt.show()
당신이 할 수있는 헤더 또는 이름을 추가하거나 숫자 인덱스로 열을 참조하십시오. 'read_csv'에 대한 매개 변수는'names'입니다. http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html – pvg