将列表列表转换为数据框中的单个列

时间:2019-05-27 13:38:30

标签: python pandas

我有一个熊猫数据框,如下所示:

comments         tags
===============================
Hello I am fine  #askfine #tag1
How are you ?    #ask # tag2

我有以下列表列表:

[['Hello', 'I', 'am', 'fine'], ['How', 'are', 'you', '?']]

现在,我想将下面的列表列表作为一列附加到原始数据框。

comments         tags             processed_comments
==============================================================
Hello I am fine  #askfine #tag1   ['Hello', 'I', 'am', 'fine']
How are you ?    #ask # tag2      ['How', 'are', 'you', '?']

我该怎么办?

df['processed_comments'] = listOfList不起作用。

谢谢

1 个答案:

答案 0 :(得分:1)

l = [['Hello', 'I', 'am', 'fine'], ['How', 'are', 'you', '?']]

您可以使用:

df['processed_comments'] = l
df
    comments         processed_comments
0   Hello I am fine [Hello, I, am, fine]
1   How are you ?   [How, are, you, ?]

每个列表元素都被视为该行的值。因此,请确保列表的长度(即列表内部的列表)等于行数。

相关问题