簡體   English   中英

將列表列表轉換為數據框中的單個列

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

我有一個熊貓數據框,如下所示:

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無法正常工作。

謝謝

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, ?]

每個列表元素都被視為該行的值。 因此,請確保列表的長度(即列表內部的列表)等於行數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM