我有一个熊猫数据框,如下所示:
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
不起作用。
谢谢
答案 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, ?]
每个列表元素都被视为该行的值。因此,请确保列表的长度(即列表内部的列表)等于行数。