Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23160

Python calculate number of words that match between two column

$
0
0

I want to calculate how many times a list of words appears in a column. Here is my dataframe:

original                           people       resultJohn is a good friend              John, Mary   1Mary and Peter are going to marry  Peter, Mary  2Bond just met the Bond girl        Bond         2Chris is having dinner             NaN          0All Marys are here                 Mary         0

I tried to use the code suggested here Check if a column contains words from another column in pandas dataframe:

import pandas as pdimport redf['result'] = [', '.join([p for p in po                      if re.search(f'\\b{p}\\b', o)) ]                for o, po in zip(df.original, df.people.str.split(',\o*'))             ]# And after I would try to calculate the number of words in column 'result'

but then I receive the following message:

error: bad escape \o at position 1

Could anyone make a suggestion?


Viewing all articles
Browse latest Browse all 23160

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>