简体   繁体   English

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

[英]convert List of List to a single column in Dataframe

I have a pandas dataframe as below: 我有一个熊猫数据框,如下所示:

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

I have the below list of lists: 我有以下列表列表:

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

Now, I wanted to append the below list of lists as a single column to the original dataframe. 现在,我想将下面的列表列表作为一列附加到原始数据框。

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

How do I do? 我该怎么办?

df['processed_comments'] = listOfList is not working. df['processed_comments'] = listOfList无法正常工作。

Thanks 谢谢

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

You can just use: 您可以使用:

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

Each list element is considered a value of the row. 每个列表元素都被视为该行的值。 So make sure that the length of the list, ie, the lists inside the list, is equal to the number of rows. 因此,请确保列表的长度(即列表内部的列表)等于行数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM