簡體   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