Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Counting word frequency in a sentence

I have two columns – one with sentences and the other with single words.

Sentence word
"Such a day! It’s a beautiful day out there" "beautiful"
"Such a day! It’s a beautiful day out there" "day"
"I am sad by the sad weather" "weather"
"I am sad by the sad weather" "sad"

I want to count the frequency of the "word" column in the "sentence" column
and achieve this output:

Sentence word n
"Such a day! It’s a beautiful day out there" "beautiful" 1
"Such a day! It’s a beautiful day out there" "day" 2
"I am sad by the sad weather" "weather" 1
"I am sad by the sad weather" "sad" 2

I tried:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

ok = []
for l in [x.split() for x in df['Sentence']]:
    for y in df['word']:
        ok.append(l.count(y))

However it does NOT stop running and takes A VERY long time, so is not feasible for my actual dataset as it has 50k rows.

Anyone can help to achieve this?

>Solution :

You can do it with zip

df['new'] = [x.count(y) for x, y in zip(df.Sentence,df.word)]
df
Out[419]: 
                                     Sentence       word  new
0  Such a day! It's a beautiful day out there  beautiful    1
1  Such a day! It's a beautiful day out there        day    2
2                 I am sad by the sad weather    weather    1
3                 I am sad by the sad weather        sad    2
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading