python panda read_table, IOError file does not exist

Question:

I have a string s to a dataframe.

s='185662748,9359839,155872098,13.99,72rn185662748,9359839,155872098,15.58,75rn185662748,9359839,155872098,126.99,94rn'

I used this df=pd.read_table(s,sep=',',lineterminator='n',header=header).

But it returns me

“IOError: File 185662748,9359839,155872098,13.99,72
…. does not exist”

I want to convert sting to output as below as direct as possible:

a     b          c       d          e

0  185662748  9359839  155872098   13.99  72

1  185662748  9359839  155872098   15.58  75

2  185662748  9359839  155872098  126.99  94
Asked By: Yang

||

Answers:

read_table expects either a filename or a file-like object. If you pass a string, it expects it to be a filename. You can wrap a string in a StringIO object to make it behave like a file instead, allowing you
to use the string directly:

import pandas as pd
from StringIO import StringIO

s='185662748,9359839,155872098,13.99,72rn185662748,9359839,155872098,15.58,75rn185662748,9359839,155872098,126.99,94rn'

pd.read_table(StringIO(s), sep=',', header=None)
Out[10]: 
           0        1          2       3   4
0  185662748  9359839  155872098   13.99  72
1  185662748  9359839  155872098   15.58  75
2  185662748  9359839  155872098  126.99  94
Answered By: Marius

Yes. a quick summary:

1) import StringIO is necessary.

2) “header” requires int, to declare column location

3) “names” declares column name.

source 2), 3):

http://pandas.pydata.org/pandas-docs/version/0.13.1/generated/pandas.io.parsers.read_table.html

Answered By: Yang
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.