Randy Tang
Randy Tang

Reputation: 4353

Convert a tab- and newline-delimited string to pandas dataframe

I have a string of the following format:

aString = '123\t456\t789\n321\t654\t987 ...'

And I would like to convert it to a pandas DataFrame

frame:
  123 456 789
  321 654 987
  ...

I have tried to convert it to a Python list:

stringList = aString.split('\n')

which results in:

stringList = ['123\t456\t789',
              '321\t654\t987',
              ...
             ]

Have no idea what to do next.

Upvotes: 12

Views: 12500

Answers (1)

It_is_Chris
It_is_Chris

Reputation: 14103

one option is list comprehension with str.split

pd.DataFrame([x.split('\t') for x in stringList], columns=list('ABC'))

     A   B   C
0   123 456 789
1   321 654 987

You can use StringIO

from io import StringIO
pd.read_csv(StringIO(aString), sep='\t', header=None)

    0   1   2
0   123 456 789
1   321 654 987

Upvotes: 20

Related Questions