从CSV列中提取关键字,然后保存到另一列

我有一堆新闻广播的文本文件,并以这种格式保存到CSV中:

id   text
001  Ad sales boost Time Warner profit\n\nQuarterly...
002  Dollar gains on Greenspan speech\n\nThe dollar...
003  Yukos unit buyer faces loan claim\n\nThe owner...
004  High fuel prices hit BA's profits\n\nBritish A...
005  Pernod takeover talk lifts Domecq\n\nShares in...

然后列表继续...

I want to iterate through the text column and find if it contains specific keywords from a list of keywords ..say, ['sales','gains', 'loan', 'forex'] and if they exist, save them in the next column as a dictionary. {key: value} where key=items from list and value= number of the times those items have occurred in that row.

输出:

id   text                                                       occurrences 
001  Ad sales boost Time Warner profit\n\nQuarterly...           {'sales':1}
002  Dollar gains sales on Greenspan speech\n\nThe dollar...     {'sales':1, 'gains':1}
003  Yukos unit buyer faces loan claim\n\nThe owner...           {'loan':1}
004  High fuel prices hit BA's profits\n\nBritish A...               NaN
005  Pernod takeover talk lifts Domecq\n\nShares in...               NaN

我的方法是什么呢?我尝试使用collections.Counter(),但无法实现所需的功能。谢谢一大堆:)

评论