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?
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']
Try this:
import StringIO
pa.read_csv(StringIO.StringIO(lines.join('n')))
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))
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?
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']
Try this:
import StringIO
pa.read_csv(StringIO.StringIO(lines.join('n')))
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))