Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert a pandas dataframe from a raw text in Python?

I have a text file containing data like this, formatted in a list, where the first element is a string containing the column names sepparated by ';', and the next elements are the value rows:

['Timestamp;T;Pressure [bar];Input line pressure [bar];Speed [rpm];Angular Position [degree];Wheel speed [rpm];Wheel angular position [degree];',
';1;5,281;5,303;219,727;10,283;216,363;45;',
';1;5,273;5,277;219,727;11,602;216,363;45;',
';1;5,288;5,293;205,078;12,832;216,363;45;',
';1;5,316;5,297;219,727;14,15;216,363;45;',
';1;5,314;5,307;219,727;15,469;216,363;45;',
';1;5,288;5,3;219,727;16,787;216,363;45;',
';1;5,318000000000001;5,31;219,727;18,105;216,363;45;',
';1;5,304;5,3;219,727;19,424;216,388;56,25;',
';1;5,291;5,29;219,947;20,742;216,388;56,25;',
';1;5,316;5,297;219,507;22,061;216,388;56,25;']

How can I convert this list of text into a pandas dataframe?

like image 871
jartymcfly Avatar asked Dec 11 '22 06:12

jartymcfly


1 Answers

Use pd.read_csv, that reads dataframe from text files, and pd.compat.StringIO, that makes stream from text, like io.StingIO:

pd.read_csv(pd.compat.StringIO("\n".join(lines)), sep=";")
like image 186
koPytok Avatar answered May 12 '23 14:05

koPytok