简体   繁体   中英

Python list to pandas dataframe

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM