Johan Nilssons Lifestream

How to read text file in Python using Pandas

I'm new to Pandas and I've been trying to do a scatter plot in Python 2.7, I've the dataset in .txt file something like this (comma separated)

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)

How to plot the above data in scatter plot without any headers on the dataset ?

I've seen in tutorials and examples that if the data set has column headings then it's possible to plot the scatter plot. I tried putting x and y as the headers for the two columns of the data set in .txt file and tried the below code.

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)

But still I'm getting error as shown below

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"

Is there a better way to deal with this (with and without header names) ?

EDIT:

The following worked for me after going through Ishan reply

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

via Stack Overflow

blog comments powered by Disqus
Get the source for phplifestream at Github