Python 将列表列表转换为Dataframe中的单个列
我有一个熊猫数据框,如下所示:Python 将列表列表转换为Dataframe中的单个列,python,pandas,Python,Pandas,我有一个熊猫数据框,如下所示: comments tags =============================== Hello I am fine #askfine #tag1 How are you ? #ask # tag2 我有以下清单: [['Hello', 'I', 'am', 'fine'], ['How', 'are', 'you', '?']] 现在,我想将下面的列表作为一列附加到原始数据框中 comments tags
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']=列表不工作
谢谢
您只需使用:
df['processed_comments'] = l
df
comments processed_comments
0 Hello I am fine [Hello, I, am, fine]
1 How are you ? [How, are, you, ?]
每个列表元素都被视为行的一个值。因此,请确保列表的长度,即列表中的列表,等于行数。df['processed_comments']=df['comments'].str.splitHello Anky,感谢您的快速响应。我有一个新的列表列表,并希望向现有的Dataframe添加一个新列。我该怎么办?已处理的_注释经过预处理,它们与注释不同。split显示的已处理的_注释与str.split完全相同,但是df['processed_comments']=listOfList应该可以工作,当你说它不工作时,你在做这件事时会遇到什么问题?如果你有一个函数可以对注释进行所有的预处理,你可以这样应用:df['processed_comments']=df['comments']。如果df和listOfList是同一个len,applyfunction应该工作。再次检查lendf和lenlistOfList是否对齐
df['processed_comments'] = l
df
comments processed_comments
0 Hello I am fine [Hello, I, am, fine]
1 How are you ? [How, are, you, ?]