我有一个看起来像这样的数据框:
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]
但是我得到的是列表的每个列表符号。 我想念什么?
答案 0 :(得分:3)
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']