Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested for loop using lambda function

I have a nested for loop something like:

for x in df['text']:
  for i in x:
    if i in someList:
      count++

Where df['text'] is a series of lists containing words such as ['word1', 'word2', 'etc']
I know I can just use the for format but I want to convert it into a lambda function.
I tried doing:
df['in'] = df['text'].apply(lambda x: [count++ for i in x if i in someList]) but it is not proper syntax. How can I modify to get the function to what I desire?

like image 549
cap Avatar asked Dec 23 '25 00:12

cap


1 Answers

I feel like you need expend the row and doing with isin , since with pandas , we usually try not use for loop .

df['in']=pd.DataFrame(df['text'].tolist(),index=df.index).isin(someList).sum(1)
like image 127
BENY Avatar answered Dec 24 '25 13:12

BENY



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!