- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想从我的数据框中的列中提取一些信息:
例子
Col
7 points — it is an example ...
13 points — as above ...
some other text ...
1 point — "what to say more?"
13 points — ...
11 points — 1234 ...
我使用 str.contain 来提取第一部分(即第一个破折号之前的所有信息,那里有。
m = (df['Col'].str.contains(r'(?i)^\d+\spoint | points'))
df[m]
我仍然得到相同的原始列(因此没有提取)。我的输出将包含两列,一列没有点信息 (Col1),另一列 (Col2) 包含提取的文本。
Col1
7 points
13 points
# need to still keep the row, even if empty
1 point
13 points
11 points
和
Col2
it is an example ...
as above ...
some other text ...
"what to say more?"
...
1234 ...
考虑第一个破折号很重要,因为文本中可能包含更多破折号。它似乎是这个符号 -
,但也许它可以是一个更长的破折号。我从我的数据集中复制和粘贴,但复制到这里似乎略有不同。
最佳答案
尝试将 str.extract
与正则表达式结合使用。
例如:
import pandas as pd
df[['Col1', 'Col2']] = df['Col'].str.extract(r"(\d+ points?)?\s*\—?\s*(.*)", expand=True)
print(df)
输出:
Col Col1 Col2
0 7 points — it is an example ... 7 points it is an example ...
1 13 points — as above ... 13 points as above ...
2 some other text ... NaN some other text ...
3 1 point — "what to say more?" 1 point "what to say more?"
4 13 points — ... 13 points ...
5 11 points — 1234 ... 11 points 1234 ...
关于python - 根据定界符吐出一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67037790/
我需要我正在构建的 clojure 应用程序的配置文件。它们应该足够容易让用户在文本编辑器中修改并且方便我的程序阅读。 我正在考虑序列化 s-表达式并使用 spit 将其放入用户主目录中的配置文件中,
我已经使用 GridLayout 几个星期了,我在打电话时注意到了这一点 gridLayout.requestLayout() 它在 LogCat 中吐出以下调试级消息: D/android.widg
我告诉 visual studio 构建一个 .dll,但无论我做什么,它只会构建一个 .lib。我将配置类型设置为 .dll,扩展名设置为 .dll,在我有“_WINDLL”的预处理器指令中,我将输
我想将 ebook-convert 与 Gitbook 一起运行,以从我的静态文件生成我自己的 pdf 文件。当我输入 ebook-convert 时,我收到了这个愚蠢的错误信息…… 我安装了 pyt
一般FindOne fmt.print result { }。我需要输出值。 我正在使用来自文档的几乎标准设置:https://docs.mongodb.com/ecosystem/drivers/g
我们已经为 Xinha 文本编辑器编写了一个插件来处理脚注。你可以看看: http://www.nicholasbs.com/xinha/examples/Newbie.html 为了处理Webkit
我在 pyramid 框架上有一个应用程序,我到处调用 request.route_url。我刚刚将我的应用程序切换到 HTTPS,所有链接仍然以 HTTP 形式出现。 是否有一个全局设置我可以放在某
我是一名优秀的程序员,十分优秀!