【发布时间】:2022-06-27 21:32:42
【问题描述】:
我有一个数据框:
import pandas as pd
data = {'id':[1,2,3],
'tokens': [[ 'in', 'the' , 'morning',
'cat', 'run', 'today', 'very', 'quick'],['dog', 'eat', 'meat', 'chicken', 'from', 'bowl'],
['mouse', 'hides', 'from', 'a', 'cat']]}
df = pd.DataFrame(data)
我还有一个索引列表。
lst_index = [[3, 4, 5], [0, 1, 2], [2, 3, 4]]
我想创建一个包含tokens 列数组中的元素的列。此外,这些元素由来自lst_index 的索引获取。所以它会是:
id tokens new
0 1 [in, the, morning, cat, run, today, very, quick] [cat, run, today]
1 2 [dog, eat, meat, chicken, from, bowl] [dog, eat, meat]
2 3 [mouse, hides, from, a, cat] [from, a, cat]
【问题讨论】: