gpt4 book ai didi

python - 计算 DataFrame 列中标点符号的数量

转载 作者:行者123 更新时间:2023-12-01 00:30:09 31 4
gpt4 key购买 nike

我正在尝试计算此 DataFrame 的 content 列中的标点符号。我试过this但它不起作用。我的数据框如下所示:

this

我希望结果是这样的: this而是用情感来数每篇文章的标点符号。

In:

text_words = df.content.str.split()
punctuation_count = {}
punctuation_count[','] = 0
punctuation_count[';'] = 0
punctuation_count["'"] = 0
punctuation_count['-'] = 0

def search_for_single_quotes(word):
single_quote = "'"
search_char_index = word.find(single_quote)
search_char_count = word.count(single_quote)
if search_char_index == -1 and search_char_count != 1:
return
index_before = search_char_index - 1
index_after = search_char_index + 1

if index_before >= 0 and word[index_before].isalpha() and index_after == len(word) - 1 and word[index_after].isalpha():
punctuation_count[single_quote] += 1

for word in text_words:
for search_char in [',', ';']:
search_char_count = word.count(search_char)
punctuation_count[search_char] += search_char_count
search_for_single_quotes(word)
search_for_hyphens(word)

Out:
AttributeError: 'list' object has no attribute 'find'

最佳答案

给定以下输入:

df = pd.DataFrame(['I love, pizza, hamberget and chips!!.', 'I like drink beer,, cofee and water!.'], columns=['content'])

content
0 I love, pizza, hamberget and chips!!.
1 I like drink beer,, cofee and water!.

试试这个代码:

count = lambda l1,l2: sum([1 for x in l1 if x in l2])

df['count_punct'] = df.content.apply(lambda s: count(s, string.punctuation))

并给出:

                                 content  count_punct
0 I love, pizza, hamberget and chips!!. 5
1 I like drink beer,, cofee and water!. 4

如果你想累积列表中每行的标点符号:

accumulate = lambda l1,l2: [x for x in l1 if x in l2]

df['acc_punct_list'] = df.content.apply(lambda s: accumulate(s, string.punctuation))

并给出:

                                 content  count_punct   acc_punct_list
0 I love, pizza, hamberget and chips!!. 5 [,, ,, !, !, .]
1 I like drink beer,, cofee and water!. 4 [,, ,, !, .]

如果你想累积字典中每一行的标点符号并将每个元素转置到数据帧列中:

df['acc_punct_dict'] = df.content.apply(lambda s: {k:v for k, v in Counter(s).items() if k in string.punctuation})

content acc_punct_dict
0 I love, pizza, hamberget and chips!!. {',': 2, '!': 2, '.': 1}
1 I like drink beer,, cofee and water!. {',': 2, '!': 1, '.': 1}

现在在 df 的列中扩展字典:

df_punct = df.acc_punct_dict.apply(pd.Series)

, ! .
0 2 2 1
1 2 1 1

如果您想将新数据框与起始数据框合并,您只需执行以下操作:

df_res = pd.concat([df, df_punct], axis=1)

并给出:

                                 content            acc_punct_dict  ,  !  .
0 I love, pizza, hamberget and chips!!. {',': 2, '!': 2, '.': 1} 2 2 1
1 I like drink beer,, cofee and water!. {',': 2, '!': 1, '.': 1} 2 1 1

注意:如果您不关心带有字典的列,可以通过df_res.drop('acc_punct_dict', axis=1)删除它

关于python - 计算 DataFrame 列中标点符号的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58252056/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com