So I have a df that looks like this:
Text
___________________________
Hello, Jim.
I knew it was Sam! Sam why?!
I have a known list of names I want to extract from each row if they appear, and append that to a new column.
I have used this code to extract the names:
t = []
df['text'].apply(lambda x: t.append([char for char in chars if char in x]))
df['characters'] = t
Which results in:
Text |Characters
_____________________________|____________
Hello, Jim. |[Jim]
|
John said it was Sam! Bad Sam|[John,Sam]
But as you can see, it hasn't counted both occourances of 'Sam'. I want it to look like this:
Text |Characters
_____________________________|____________
Hello, Jim. |[Jim]
|
John said it was Sam! Bad Sam|[John,Sam,Sam]
Then I will be able to so a simple count for each item in the list for each row.
I'm not super familiar with lambda functions, and alot of this doesn't feel very efficient.
Any ideas?
Edit:
I can do this to get a count of one specific name for each row:
df['char_count'] = df['text'].apply(lambda x: x.count('Sam'))
But not sure how to pass in the list in the column I have generated.