0

how do I separate a list of string and turn it into a dataframe? From this format:

['x data,y data',
 '-969.0,-52.12282,',
 '-959.0,-49.436077,',
 '-948.0,-46.615,',
 '-938.0,-44.59994,',
 ]

To a dataframe like this(The first row will be the dataframe column header):

enter image description here

6 Answers 6

3

You could do:

import pandas as pd
import io

lst = ['x data,y data',
       '-969.0,-52.12282,',
       '-959.0,-49.436077,',
       '-948.0,-46.615,',
       '-938.0,-44.59994,',
       ]

df = pd.read_csv(io.StringIO('\n'.join(s[:-1] for s in lst)))
print(df)

Output

   x data      y dat
0  -969.0 -52.122820
1  -959.0 -49.436077
2  -948.0 -46.615000
3  -938.0 -44.599940
Sign up to request clarification or add additional context in comments.

6 Comments

Why the generator? Can't you just '\n'.join(lst)?
Need to remove the trailing comma
Without removing trailing comma works, give a try to my answer, just add index_col=False
I made some adjustment for your solution and it deliver the result that i want. df = pd.read_csv(io.StringIO('\n'.join(s for s in data[0])),index_col=False)
@wkwkwk if you are using index_col=False then you can just use '\n'.join(lst)
|
2

My try using io.StringIO+.read_csv:

s = ['x data,y data',
 '-969.0,-52.12282,',
 '-959.0,-49.436077,',
 '-948.0,-46.615,',
 '-938.0,-44.59994,',
 ]
import pandas as pd
import io
df = pd.read_csv(io.StringIO('\n'.join(s)),index_col=False)

Comments

1

Idea in list comprehension:

import ast

df = pd.DataFrame([ast.literal_eval(x) for x in data[1:]], columns=data[0].split(','))  

print (df)
   x data     y data
0  -969.0 -52.122820
1  -959.0 -49.436077
2  -948.0 -46.615000
3  -938.0 -44.599940

Or:

df = pd.DataFrame([x.strip(',').split(',') for x in data[1:]], columns=data[0].split(','))  

Comments

0

Solution from Dani Mesejo and Wasif Hasan

import io


   list = ['x data,y data',
 '-970.0,-34.12164,',
 '-959.0,-32.37526,',
 '-949.0,-30.360199,',
 ]
df = pd.read_csv(io.StringIO('\n'.join(data[0])),index_col=False)

Output:

   x data    y data
0  -970.0 -34.121640
1  -959.0 -32.375260
2  -949.0 -30.360199

Comments

0

You could do the following:

import pandas
z = ['x data,y data',
       '-969.0,-52.12282,',
       '-959.0,-49.436077,',
       '-948.0,-46.615,',
       '-938.0,-44.59994,',
       ]
df = []

# Split based on commas

for i in z:
    df.append(i.split(',')[0:2])

# Convert to Dataframe

final_df = pandas.DataFrame(df[1:], columns=df[0])

I splited the data based on commas by using the split command.

Comments

0

A pretty simple way to do it would be :-

lst = ['x data,y data',
       '-969.0,-52.12282,',
       '-959.0,-49.436077,',
       '-948.0,-46.615,',
       '-938.0,-44.59994,',
       ]
lst[0]=lst[0]+','
import pandas as pd
import numpy as np
#Reshape it having 2 columns
lst=list(map(lambda x: x[:-1].split(','), lst))
np_array_1=np.array(lst).reshape(-1,2)
#Create DataFrame
df=pd.DataFrame(data=np_array_1[1:,:],columns=np_array_1[0,:])
#You may convert data type now

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.