连接df列中的列表

时间:2018-11-09 14:05:53

标签: python python-3.x list

我有一个看起来像这样的数据框:

import pandas as pd
df = pd.DataFrame({'id': [1,2,3], 'tokens': [["anna","brings","work","life"], ["apples","bananas"], ["sun","trial","once"]]})

我想做的就是将列中列表中的所有字符串简单地连接到一个列表中,以使结果列表如下所示:

result_list = ["anna","brings","work","life", "apples","bananas","sun","trial","once"]

我尝试遍历每一行,然后遍历列表中的每个元素:

result_list = [item for sublist in df.tokens for item in sublist]

但是我得到的是列表的每个列表符号。 我想念什么?

4 个答案:

答案 0 :(得分:3)

使用chain.from_iterable

import pandas as pd

from itertools import chain

df = pd.DataFrame(
    {'id': [1, 2, 3], 'tokens': [["anna", "brings", "work", "life"], ["apples", "bananas"], ["sun", "trial", "once"]]})

result = list(chain.from_iterable(df.tokens))
print(result)

答案 1 :(得分:1)

your_list = []
for i in range (0, len(df)):
    for j in range (0, len(df['tokens'][i])):
       your_list.append(df['tokens'][i][j])
your_list

答案 2 :(得分:0)

您可以使用嵌套列表推导。

>>> [x for sub in df['tokens'] for x in sub]                                
['anna', 'brings', 'work', 'life', 'apples', 'bananas', 'sun', 'trial', 'once']

edit:相当于您的尝试,我错过了。你所做的应该很好。

答案 3 :(得分:0)

a = []
[a.extend(i) for i in df.tokens]
print(a)

输出:

['anna', 'brings', 'work', 'life', 'apples', 'bananas', 'sun', 'trial', 'once']