简体   繁体   中英

Create a list of lists stored as objects in a single column

I have lists stored as objects in a column of data. I need to create a single list from these 'lists' but they are not being recognized as a list.

I have tried converting column to a list, concatenating, creating a series but the results are not treated as a list.

What I have:

code1
Out[83]: 
0    ['hair', 'body']
1    ['hair', 'body']
2    ['hair', 'body']
Name: personal_interests, dtype: object

code1.tolist()
Out[79]: ["['hair', 'body']", "['hair', 'body']", "['hair', 'body']"]

What I need:

example = [['hair', 'body'],
           ['hair', 'body'],
           ['hair', 'body']]
example
Out[94]: [['hair', 'body'], ['hair', 'body'], ['hair', 'body']]

The following solution evaluates the list inside the string and appends to a new empty list:

from ast import literal_eval
l1 = ["['hair', 'body']", "['hair', 'body']", "['hair', 'body']"]
l2 = []
for i in l1:
    l2.append(literal_eval(i))
l2 
#[['hair', 'body'], ['hair', 'body'], ['hair', 'body']]

I tried to reproduce the problem by passing the lists as strings instead of pure lists:

df= pd.DataFrame({'a':["['hair', 'body']", "['hair', 'body']"]})
df
        a
0   ['hair', 'body']
1   ['hair', 'body']

As you will notice, the elements in the dataframe do not show as strings but as normal lists. When I convert the series to list, the elements are represented as strings, as expected:

df['a'].tolist()
#["['hair', 'body']", "['hair', 'body']"]

So now if we apply literal_eval on all elements and then convert to list, we get the desired results.

df['a'].apply(literal_eval).tolist()
#[['hair', 'body'], ['hair', 'body']]

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