gpt4 book ai didi

python - 如何从数据框中计算每篇文章的段落?

转载 作者:行者123 更新时间:2023-12-04 10:08:31 25 4
gpt4 key购买 nike

我想从数据框中计算段落。但是,事实证明我的结果在列表中变为零。有人知道如何解决吗?非常感谢。

这是我的代码:

def count_paragraphs(df):
paragraph_count = []
linecount = 0
for i in df.text:
if i in ('\n','\r\n'):
if linecount == 0:
paragraphcount = paragraphcount + 1
return paragraph_count

count_paragraphs(df)

df.text
0     On Saturday, September 17 at 8:30 pm EST, an e...
1 Story highlights "This, though, is certain: to...
2 Critical Counties is a CNN series exploring 11...
3 McCain Criticized Trump for Arpaio’s Pardon… S...
4 Story highlights Obams reaffirms US commitment...
5 Obama weighs in on the debate\n\nPresident Bar...
6 Story highlights Ted Cruz refused to endorse T...
7 Last week I wrote an article titled “Donald Tr...
8 Story highlights Trump has 45%, Clinton 42% an...
9 Less than a day after protests over the police...
10 I woke up this morning to find a variation of ...
11 Thanks in part to the declassification of Defe...
12 The Democrats are using an intimidation tactic...
13 Dolly Kyle has written a scathing “tell all” b...
14 The Haitians in the audience have some newswor...
15 The man arrested Monday in connection with the...
16 Back when the news first broke about the pay-t...
17 Chicago Environmentalist Scumbags\n\nLeftists ...
18 Well THAT’S Weird. If the Birther movement is ...
19 Former President Bill Clinton and his Clinton ...
Name: text, dtype: object

最佳答案

使用 Series.str.count :

def count_paragraphs(df):
return df.text.str.count(r'\n\n').tolist()
count_paragraphs(df)

关于python - 如何从数据框中计算每篇文章的段落?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61452674/

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