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?
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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments