一个字符在数据框形式的文本中出现了多少次

How many times a character appears in a text of a dataframe form

我是 NLP 的初学者,我有一个具有以下形式的数据框

text                         label 
----                        -----
This is he # first text     first label
This is the # second text   second label 
....                         ....

我想计算字符“#”在此数据框中的应用次数。请你帮助我好吗?我正在寻找一个通用代码,我可以计算出“#”或另一个字符或一个词。

也许这不是最佳答案:

def count(text, target):
    words = text.split()
    counter = 0
    for word in words:
        if word == target:
            counter += 1
    return counter
df['counter'] = df.apply(lambda row: count(row['text'], target="#"),axis=1)
sum(df["counter"])

dt['text'].apply(lambda x: str.count(x, '#')).sum()