python - 关于 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})') 不知道是什么。
解决方案
事实证明,Pycharm 将df_new_8 = dfnew['text'].str.extract(r'(?P\d{4})')作为 DataFrame 返回,而 Jupyter notebook 将其作为系列返回。
推荐阅读
- asp.net-core - 类库中 .NET Core 中的依赖注入
- javascript - 为什么我没有在 JS 中得到 console.log
- jenkins - Azure 管道 UI 接受参数(如 Jenkins)
- java - java.lang.ClassNotFoundException 运行时尝试使用 appium 打开 android 模拟器
- r - 在列表中查找共享元素
- c# - 我的程序需要使用位于 Debug 文件夹中的第三方软件,但程序显示找不到配置文件。C#
- yocto - 检查配方中的内核版本
- r - R data.frame 列到单个列表中
- amp-html - 谷歌优化和 AMP 集成
- jenkins - Jenkins 构建到远程服务器