Load data from a list of lines using pandas

Question:

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?

Asked By: juniper-

||

Answers:

I doubt you’d need Pandas to do this, you can do this just with normal Python:

with open('data.txt', 'r') as f:
    lines = [l.rstrip() for l in f.readlines()]

print(lines)

The output:

['col1 col2', '1 2', '3 4']
Answered By: Games Brainiac

Try this:

import StringIO

pa.read_csv(StringIO.StringIO(lines.join('n')))
Answered By: Bernhard

more modern (python 3x moved StringIO into io, and the syntax of join changed):

import pandas as pa
from io import StringIO

pa.read_csv(StringIO('n'.join(lines))
Answered By: wheelreinventor
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.