繁体   English   中英

遍历每行中的每个单词并删除列表中的单词

[英]Loop over each word in each row and remove words if in a list

我在 dataframe 中有以下一列(每一行都是一个人,每个单元格中都有一个标记化单词列表)。

Q395_R

[due, car, accident, year, ago, medical, condi...
[spending, time, loved, one, commute, able, co...
[initially, understanding, need, lockdown, ero...
[time, focus, exercise, le, sport, do, poured,..
[spending, time, family, realisation, need, ru...

我还有一个单词列表:

words395 = ['rising',
 'accident',
 'le',
 'lasted',
 'understanding',
 'spending',
 'adopted',
 'raising',
 'fabulous',
 'loneliness',
 'contract',....]

我想创建一个 function

  1. 遍历每一行中的每个人
  2. 遍历每一行中的每个单词
  3. 如果单词在列表 words395 中,则删除每个单元格中的单词

我不确定如何通过每个人和单词创建两个循环到 go,有人可以帮忙吗?

预期结果:

Q395_R
    
[due, car, year, ago, medical, condi...
[time, loved, one, commute, able, co...
[initially, need, lockdown, ero...
[time, focus, exercise, sport, do, poured,..
[time, family, realisation, need, ru...

使用 lambda function 将值转换为列表到集合:

s = set(words395)
df['Q395_R'] = df['Q395_R'].apply(lambda x: [y for y in x if y not in s])

暂无
暂无

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

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