Reputation: 874
I have a list that follows this format:
a=['date name','10150425010245 name1','10150425020245 name2']
I am trying to convert this to Pandas df:
newlist=[]
for item in a:
newlist.append(item.split(' '))
Now, convert this to df:
pd.DataFrame(newlist)
which results in
0 1
0 date name
1 10150425010245 name1
2 10150425020245 name2
I want to have 'date' and 'name' as header, but I can't manage to do that. Is there a more efficient way to automatically convert a list of strings into a dataframe than this?
Upvotes: 2
Views: 8425
Reputation: 16154
Tempted to summarise the answers already given in one line:
a=['date name','10150425010245 name1','10150425020245 name2']
pd.DataFrame(
map(str.split, a)[1:],
columns=a[0].split(),
)
Output:
Out[8]:
date name
0 10150425010245 name1
1 10150425020245 name2
Upvotes: 1
Reputation: 10913
you were on the right track. With slight modification, your code works fine.
import pandas as pd
a=['date name','10150425010245 name1','10150425020245 name2']
newlist=[]
for item in a:
newlist.append(item.split(' '))
newlist2=pd.DataFrame(newlist,columns=["date","name"])[1:]
newlist2
date name
10150425010245 name1
10150425020245 name2
Upvotes: 4
Reputation: 77027
Here's one approach.
Use list comprehensions instead of loops.
In [160]: data = [x.split('') for x in a]
In [161]: data
Out[161]: [['date', 'name'], ['10150425010245', 'name1'], ['10150425020245', 'name2']]
Then use data[1:]
as values and data[0]
as column names.
In [162]: pd.DataFrame(data[1:], columns=data[0])
Out[162]:
date name
0 10150425010245 name1
1 10150425020245 name2
Upvotes: 4