Python 根据内容重新格式化字符串
Python reformatting strings based on contents
在 pandas 数据框中,我的行的内容格式如下:
1) abc123-Target 4-ufs
2) abc123-target4-ufs
3) geo.4
4) j123T4
所有这些都应该很简单:目标 4
目前我的清洁程序如下:
df["point_id"] = df["point_id"].str.lower()
df["point_id"] = df['point_id'].str.replace('^.*?(?=target)', '')
这个returns:
1) target 4-ufs
2) target4-ufs
3) geo.14
4) geo.2
5) j123T4
我认为我需要的是:
a. Remove anything after the last number in the string, this solves 1
b. If 'target' does not have a space after it add a space, this with the above solves 2
c. If the string ends in a point and a number of any length remove everything before the point (incl. point) and replace with 'target ', this solves 3 and 4
d. If the string ends with a 't' followed by a number of any length remove everything before 't' and replace with 'target ', this solves 5
我正在查看正则表达式和 re,但以下内容无效(在最后一个数字前添加 space)
df["point_id"] = re.sub(r'\D+$', '', df["point_id"])
阅读规则,您可以使用 2 个捕获组并检查组值:
\btarget\s*(\d+)|.*[t.](\d+)$
\btarget\s*(\d+)
匹配目标,可选的空白字符并捕获 组 1 中的 1+ 个数字
|
或
.*[t.]
匹配 0+ 个字符后跟 t 或 a .
(\d+)$
在字符串末尾的 组 2 中捕获 1+ 个数字
Python 示例:
import re
import pandas as pd
pattern = r"\btarget\s*(\d+)|.*[t.](\d+)$"
strings = [
"abc123-Target 4-ufs",
"abc123-target4-ufs",
"geo.4",
"j123T4"
]
df = pd.DataFrame(strings, columns=["point_id"])
def change(s):
m = re.search(pattern, s, re.IGNORECASE)
return "target " + (m.group(2) if m.group(2) else m.group(1))
df["point_id"] = df["point_id"].apply(change)
print(df)
输出
point_id
0 target 4
1 target 4
2 target 4
3 target 4
您可以使用
df = pd.DataFrame({'point_id':['abc123-Target 4-ufs','abc123-target4-ufs','geo.4','j123T4']})
df['point_id'] = df['point_id'].str.replace(r'(?i).*Target\s*(\d+).*', r'target ', regex=True)
df.loc[df['point_id'].str.contains(r'(?i)\w[.t]\d+$'), 'point_id'] = 'target 4'
# point_id
# 0 target 4
# 1 target 4
# 2 target 4
# 3 target 4
正则表达式是 (?i)Target\s*\d+|\w+[.t]\d+$
:
(?i)
- 不区分大小写的匹配
.*
- 除换行字符外的任何 0+ 个字符,尽可能多
Target\s*(\d+).*
- Target
、零个或多个空格以及捕获到第 1 组的一个或多个数字
.*
- 除换行字符外的任何 0+ 个字符,尽可能多
第二个正则表达式匹配
(?i)
- 不区分大小写的匹配
\w
- 一个字符字符,然后
[.t]
- 一个 .
或 t
然后
\d+$
- 字符串末尾的一位或多位数字。
第二个正则表达式用作掩码,只要模式与正则表达式匹配,point_id
列中的值就会设置为 target 4
。
参见 regex #1 demo and regex #2 demo。
在 pandas 数据框中,我的行的内容格式如下:
1) abc123-Target 4-ufs
2) abc123-target4-ufs
3) geo.4
4) j123T4
所有这些都应该很简单:目标 4
目前我的清洁程序如下:
df["point_id"] = df["point_id"].str.lower()
df["point_id"] = df['point_id'].str.replace('^.*?(?=target)', '')
这个returns:
1) target 4-ufs
2) target4-ufs
3) geo.14
4) geo.2
5) j123T4
我认为我需要的是:
a. Remove anything after the last number in the string, this solves 1
b. If 'target' does not have a space after it add a space, this with the above solves 2
c. If the string ends in a point and a number of any length remove everything before the point (incl. point) and replace with 'target ', this solves 3 and 4
d. If the string ends with a 't' followed by a number of any length remove everything before 't' and replace with 'target ', this solves 5
我正在查看正则表达式和 re,但以下内容无效(在最后一个数字前添加 space)
df["point_id"] = re.sub(r'\D+$', '', df["point_id"])
阅读规则,您可以使用 2 个捕获组并检查组值:
\btarget\s*(\d+)|.*[t.](\d+)$
\btarget\s*(\d+)
匹配目标,可选的空白字符并捕获 组 1 中的 1+ 个数字
|
或.*[t.]
匹配 0+ 个字符后跟 t 或 a.
(\d+)$
在字符串末尾的 组 2 中捕获 1+ 个数字
Python 示例:
import re
import pandas as pd
pattern = r"\btarget\s*(\d+)|.*[t.](\d+)$"
strings = [
"abc123-Target 4-ufs",
"abc123-target4-ufs",
"geo.4",
"j123T4"
]
df = pd.DataFrame(strings, columns=["point_id"])
def change(s):
m = re.search(pattern, s, re.IGNORECASE)
return "target " + (m.group(2) if m.group(2) else m.group(1))
df["point_id"] = df["point_id"].apply(change)
print(df)
输出
point_id
0 target 4
1 target 4
2 target 4
3 target 4
您可以使用
df = pd.DataFrame({'point_id':['abc123-Target 4-ufs','abc123-target4-ufs','geo.4','j123T4']})
df['point_id'] = df['point_id'].str.replace(r'(?i).*Target\s*(\d+).*', r'target ', regex=True)
df.loc[df['point_id'].str.contains(r'(?i)\w[.t]\d+$'), 'point_id'] = 'target 4'
# point_id
# 0 target 4
# 1 target 4
# 2 target 4
# 3 target 4
正则表达式是 (?i)Target\s*\d+|\w+[.t]\d+$
:
(?i)
- 不区分大小写的匹配.*
- 除换行字符外的任何 0+ 个字符,尽可能多Target\s*(\d+).*
-Target
、零个或多个空格以及捕获到第 1 组的一个或多个数字.*
- 除换行字符外的任何 0+ 个字符,尽可能多
第二个正则表达式匹配
(?i)
- 不区分大小写的匹配\w
- 一个字符字符,然后[.t]
- 一个.
或t
然后\d+$
- 字符串末尾的一位或多位数字。
第二个正则表达式用作掩码,只要模式与正则表达式匹配,point_id
列中的值就会设置为 target 4
。
参见 regex #1 demo and regex #2 demo。