2

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?

3 Answers 3

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This works. It should be x.split(' '), but other than that it works.
4

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

Comments

1

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

2 Comments

Not quite. Two issues here. 1) Output will have column names repeated there, rather it should be pd.DataFrame( map(str.split, a)[1:],columns=['date', 'name']) 2) Column names should rather taken from list than manually passing it.
The index starting at 1 was a bother. Updated it with your 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.