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)


import pandas as pd
import matplotlib.pyplot as mplt

# Taking Dataset using Pandas

input_data = pd.read_csv('data.txt');

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');
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\", line 16, in <module>
    y_value = input_data[['y']]
  File "C:\Python27\lib\site-packages\pandas\core\", line 1791, in __getitem__
    return self._getitem_array(key)
  File "C:\Python27\lib\site-packages\pandas\core\", line 1835, in _getitem_array
    indexer = self.ix._convert_to_indexer(key, axis=1)
  File "C:\Python27\lib\site-packages\pandas\core\", 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) ?


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)

via Stack Overflow

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