Python 数据帧中特定关键字的计数

Python 数据帧中特定关键字的计数,python,pandas,Python,Pandas,我有这样一个数据帧: A 0 Please wait outside of the house 1 A glittering gem is not enough. 2 The memory we used to share is no longer coher... 3 She only paints with bold colors; she does not... 我有一组关键词: keywords = ["of","is","she"] 如何为每个关键字创建一列

我有这样一个数据帧:

    A
0   Please wait outside of the house
1   A glittering gem is not enough.
2   The memory we used to share is no longer coher...
3   She only paints with bold colors; she does not...
我有一组关键词:

keywords = ["of","is","she"]
如何为每个关键字创建一列,其中包含该关键字在dataframe的每个句子中出现的次数?它看起来像:

                                                   A  of  is  she
0                   Please wait outside of the house   1   0    0
1                    A glittering gem is not enough.   0   1    0
2  The memory we used to share is no longer coher...   0   1    0
3  She only paints with bold colors; she does not...   0   0    2

注意:我查看了,但它没有回答我的问题。

我假设您正在查找不区分大小写的匹配项

将熊猫作为pd导入
df=pd.DataFrame({
“A”:[
“请在屋外等候”,
“光有闪闪发光的宝石是不够的。”,
“我们过去共享的记忆不再连贯…”,
“她只画大胆的颜色;她不……”
]
})
关键词=[“of”,“is”,“she”]
对于关键字中的关键字:
df[keyword]=df['A'].apply(lambda\u str:\u str.lower().count(关键字))
打印(df)
输出

                                                   A  of  is  she
0                   Please wait outside of the house   1   0    0
1                    A glittering gem is not enough.   0   1    0
2  The memory we used to share is no longer coher...   0   1    0
3  She only paints with bold colors; she does not...   0   0    2

您也可以这样做:

df['is'] = df.A.str.count(r'is', flags=re.IGNORECASE)
df['of'] = df.A.str.count(r'of', flags=re.IGNORECASE)
df['she'] = df.A.str.count(r'she', flags=re.IGNORECASE)


                                                   A  of  is  she
0                   Please wait outside of the house   1   0    0
1                    A glittering gem is not enough.   0   1    0
2  The memory we used to share is no longer coher...   0   1    0
3  She only paints with bold colors; she does not...   0   0    2