首页 > 解决方案 > 创建作为对象存储在单列中的列表列表

问题描述

我将列表存储为数据列中的对象。我需要从这些“列表”中创建一个列表,但它们没有被识别为列表。

我尝试将列转换为列表、连接、创建系列,但结果不被视为列表。

我有的:

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']"]

我需要的:

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

标签: pythondataframe

解决方案


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']]

推荐阅读