Issue
I'd like to load a pandas data frame from a list of text lines, much like I would do using the pa.read_csv() function.
So if I have a text file like this:
col1 col2
1 2
3 4
I can load it into a pandas data frame like this:
>>> import pandas as pa
>>> pa.read_csv('test.txt')
col1 col2
0 1 2
1 3 4
My question is, can I load an array like this:
>>> lines = ['col1 col2', '1 2', '3 4']
And use some function to load that as a dataframe, as in the case with the file?
Solution
Try this:
import StringIO
pa.read_csv(StringIO.StringIO(lines.join('\n')))
Answered By - Bernhard
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.