I have a string like this:
txt = 'A AGILENT TECH INC \nAA ALCOA INC '
And want to obtain a DataFrame
like this:
In [185]: pd.DataFrame({'col1':['A','AA'],'col2':['AGILENT TECH INC','ALCOA INC']})
Out[185]:
col1 col2
0 A AGILENT TECH INC
1 AA ALCOA INC
I tried so far:
from StringIO import StringIO
import re
pd.DataFrame.from_csv(StringIO(re.sub(' +\n', ';', txt)), sep=';')
Out[204]:
Empty DataFrame
Columns: [AA ALCOA INC ]
Index: []
But the result is not the one expected. It seems I do not handle all optionality of from_csv
or StringIO
.
It is certainly linked to this question.