首页 > 解决方案 > 关于 extract() 函数的 Jupyter Notebook 命名组问题

问题描述

我正在构建一个从文本文档中提取日期的文本分析程序。日期有多种格式,所以我写了几个提取行,如下所示。

df_new_1 = dfnew['text'].str.extract(r'(?P\d{1,2})/-/-')

df_new_7 = dfnew['text'].str.extract(r'(?P\d{1,2})/-')

df_new_8 = dfnew['text'].str.extract(r'(?P\d{4})')

在 PyCharm 中运行这些行时,它们按预期工作。但是,在 Jupyter Notebook 中运行这些时,最后一行 (df_new_8) 不显示分组名称。

df_new_8.tail() 的输出 PyCharm:年份 495 1979 496 2006 497 2008 498 2005 499 1980

df_new_8.tail() 的输出 Jupyter Notebook 没有列标题!!:Out[1] 495 1979 496 2006 497 2008 498 2005 499 1980 名称:年份,dtype:对象

为了说明,在 Jupyter 中运行 df_new_1.head() 时,它输出(如预期的那样): Out[3] Month Day Year 0 03 25 93 1 6 18 85 2 7 8 71 3 9 27 75 4 2 6 96

在程序结束时,我使用 fillna 将所有 df_new* 数据帧“连接”在一起。df_out = df_new_1.fillna(df_new_2).fillna(df_new_3).fillna(df_new_4).fillna(df_new_5).fillna(df_new_6).fillna(df_new_7).fillna(df_new_8).fillna(1)

那时真正的问题出现在 Jupyter Notebook 中。显然 df_new_8 没有被处理,可能是因为缺少命名组。同样,这在 PyCharm 中运行良好。所以,本能地我说这条线有问题: df_new_8 = dfnew['text'].str.extract(r'(?P\d{4})') 不知道是什么。

标签: pythonjupyter-notebookregex-group

解决方案


事实证明,Pycharm 将df_new_8 = dfnew['text'].str.extract(r'(?P\d{4})')作为 DataFrame 返回,而 Jupyter notebook 将其作为系列返回。


推荐阅读