python-3.x - 用 % 按数字拆分 pandas 列

标签 python-3.x regex

我有一个 df,其中一列如下所示:

**Share**
We are safe 25%
We are always safe 12.50% (India Aus, West)
We are ok (USA, EU)
We are not OK
What is this
Always wise 25.66%

我想拆分此列,以便将适用的 % 值从该列拆分为新的值。 所以输出将是

Share                  Percent    LOCATION
We are safe            25%  
We are always safe     12.50%     India Aus, West
We are ok                         USA, EU
We are not OK
What is this
Always wise            25.66%

我认为下面会将其从右侧拆分,但它不起作用

df['Percent'] = df['Share'].str.rsplit(r' \d',1).str[0]

最佳答案

您可以提取这些值:

df[['Share','Percent']] = df['Share'].str.split(r'\s+(?=\d+(?:\.\d+)?%\s*$)',expand=True).fillna("")

Pandas 测试:

import pandas as pd
df = pd.DataFrame({'Share':['We are safe 25%','We are ok', 'We are always safe 12.50%']})
df[['Share','Percent']] = df['Share'].str.split(r'\s+(?=\d+(?:\.\d+)?%\s*$)',expand=True).fillna("")
>>> df
                Share Percent
0         We are safe     25%
1           We are ok        
2  We are always safe  12.50%

请参阅regex demo 。详情:

  • \s+ - 一个或多个空格
  • (?=\d+(?:\.\d+)?%\s*$) - 与紧随其后的位置匹配的正向前瞻:
    • \d+ - 一位或多位数字
    • (?:\.\d+)? - 可选的 . 序列和一个或多个数字
    • % - % 符号
    • \s* - 0 个或多个尾随(接下来是 $)空格和
    • $ - 字符串结尾。

关于python-3.x - 用 % 按数字拆分 pandas 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64334963/

相关文章:

python - 如何输入泛型函数?

python - 在Python中跳过yield

python - 如何将字节串转换为字符串?

regex - 我需要一个正则表达式来删除特定字符之前的空格

python - 将 Pandas DF 转换为 Numpy Array 在尝试预测时会出现 # of features 错误?

python-3.x - 将 image_to_osd 方法与 pytesseract 结合使用时出错

c# - 作为匹配的一部分返回常量值的正则表达式

regex - 匹配 Google 内容类别的正则表达式

java - 在java中使用正则表达式从字符串中提取 double 或整数?

regex - 为什么这个简单的 bash 正则表达式不返回 true?