1

我在python中有一个数据框

word    string                                               count    wordSummary
apple    One apple price is 40
apple    one apple price is 50 and three apples price are 60 but apple....

我想计算字符串中的确切单词以及单词之前的 5char 和单词之后的 5chars

word    string                                                         count  wordSummary 
apple    One apple price is 40                                            1   Once apple price
apple    one apple price is 50 and three apples price are 60 but apple    2  one apple price

我尝试了以下部分,但显示错误。

df["count"] = df["string"].count(df["word"])

4

1 回答 1

1

尝试申请:

df['count'] = df.apply(lambda row: row['string'].lower().split(' ').count(row['word'].lower()), axis=1)
于 2020-03-30T15:11:15.963 回答