python - 使用熊猫数据框的正则表达式
问题描述
输入 csv 文件:
_id,field_name,field_friendly_name,purpose_of_use,category,data_source,schema,table,attribute_type,sample_values,mask_it,is_included_in_report
5e95a49b0985567430f8fc00,FullName,,,,,,,,,,
5e95a4dd0985567430f9ef16,xyz,,,,,,,,,,
5e95a4dd0985567430f9ef17,FullNm,,,,,,,,,,
5e95a4dd0985567430f9ef18,FirstName,,,,,,,,,,
5e95a49b0985567430f8fc01,abc,,,,,,,,,,
5e95a4dd0985567430f9ef19,FirstNm,,,,,,,,,,
5e95a4dd0985567430f9ef20,LastName,,,,,,,,,,
5e95a4dd0985567430f9ef21,LastNm,,,,,,,,,,
5e95a49b0985567430f8fc02,LegalName,,,,,,,,,,
5e95a4dd0985567430f9ef22,LegalNm,,,,,,,,,,
5e95a4dd0985567430f9ef23,NickName,,,,,,,,,,
5e95a4dd0985567430f9ef24,pqr,,,,,,,,,,
5e95a49b0985567430f8fc03,NickNm,,,,,,,,,,
正则表达式 csv 表:
Personal_Inforamtion,regex,addiitional_grep
Full Name,full|name|nm|txt|dsc,full
First Name,first|name|nm|txt|dsc,first
Last Name,last|name|nm|txt|dsc,last
Legal Name,legal|name|nm|txt|dsc,legal
Nick Name,nick|name|nm|txt|dsc,nick
我的代码
包括python模块
import pandas as pd
import re
从 csv 文件定义数据框
df = pd.read_csv("Default-Profile.csv")
将系列 field_name 上的下划线 (_) 和连字符 (-) 替换为 df
df.field_name = df.field_name.str.replace("[_-]", "", regex=True)
使用 df 将系列 field_name 上的所有字符更改为小写
df.field_name = df.field_name.str.lower()
定义正则表达式表
regex_table = pd.read_csv("regex.csv")
代码是更新 field_friendly_name && is_included_in_report
在 df.field_name 中为正则表达式表中的每个正则表达式查找模式,如果找到正确匹配,则使用 Personal_information 更新列 field_friendly_name,如果不更新为 not_found,如果找到匹配,如果不是 False,则更新最后一列为 True。
EX:单词应该只由 full|name|nm|txt|dsc 组成,并且应该包含 full
Personal_Inforamtion,regex,addiitional_grep
Full Name,full|name|nm|txt|dsc,full
然后更新 df 如下:
_id,field_name,field_friendly_name,purpose_of_use,category,data_source,schema,table,attribute_type,sample_values,mask_it,is_included_in_report
5e95a49b0985567430f8fc00,FullName,Full Name,,,,,,,,,TRUE
5e95a4dd0985567430f9ef16,xyz,not_found,,,,,,,,,FALSE
5e95a4dd0985567430f9ef17,FullNm,Full Name,,,,,,,,,TRUE
期望的输出
_id,field_name,field_friendly_name,purpose_of_use,category,data_source,schema,table,attribute_type,sample_values,mask_it,is_included_in_report
5e95a49b0985567430f8fc00,FullName,Full Name,,,,,,,,,TRUE
5e95a4dd0985567430f9ef16,xyz,not_found,,,,,,,,,FALSE
5e95a4dd0985567430f9ef17,FullNm,Full Name,,,,,,,,,TRUE
5e95a4dd0985567430f9ef18,FirstName,First Name,,,,,,,,,TRUE
5e95a49b0985567430f8fc01,abc,not_found,,,,,,,,,FALSE
5e95a4dd0985567430f9ef19,FirstNm,First Name,,,,,,,,,TRUE
5e95a4dd0985567430f9ef20,LastName,Last Name,,,,,,,,,TRUE
5e95a4dd0985567430f9ef21,LastNm,Last Name,,,,,,,,,TRUE
5e95a49b0985567430f8fc02,LegalName,Legal Name,,,,,,,,,TRUE
5e95a4dd0985567430f9ef22,LegalNm,Legal Name,,,,,,,,,TRUE
5e95a4dd0985567430f9ef23,NickName,NickName,,,,,,,,,TRUE
5e95a4dd0985567430f9ef24,pqr,not_found,,,,,,,,,FALSE
5e95a49b0985567430f8fc03,NickNm,NickName,,,,,,,,,TRUE
解决方案
作为替代方案,您可以使用正则表达式表文件最后一列中的单词创建一组正则表达式
(full)|(first)|(last)|(legal)|(nick)
您仍然可以根据需要调整正则表达式表的最后一列以获得更具体的输出。然后,您可以将not_found
case 附加到正则表达式数据框以准备要与 一起使用的数据str.extract
,从而从第一个匹配模式中提取组。通过组匹配,我们可以idxmax
在行轴上获得正则表达式组索引。之后,使用组索引信息将正则表达式表第一列的信息映射到df数据帧。
import pandas as pd
import re
df = pd.read_csv("data.csv")
print(df)
regxt = pd.read_csv("regex_table.csv")
print(regxt)
# append not_found item case
not_found = pd.Series(["not_found","",""], index=regxt.columns)
regxt = regxt.append(not_found, ignore_index=True)
# create regex groups with last column csv words
regxl = regxt.iloc[:, 2].to_list()
regx_grps = "|".join(["(" + i + ")" for i in regxl])
# get regex group match index
grp_match = df["field_name"].str.extract(regx_grps, flags=re.IGNORECASE)
grp_idx = (~grp_match.isnull()).idxmax(axis=1)
df["field_friendly_name"] = grp_idx.map(lambda r: regxt.loc[r, "Personal_Inforamtion"])
df["is_included_in_report"] = grp_idx.map(lambda r: str(r!=len(regxt)-1).upper())
print(df)
来自df的输出
_id field_name field_friendly_name ... mask_it is_included_in_report
0 5e95a49b0985567430f8fc00 FullName Full Name ... NaN TRUE
1 5e95a4dd0985567430f9ef16 xyz not_found ... NaN FALSE
2 5e95a4dd0985567430f9ef17 FullNm Full Name ... NaN TRUE
3 5e95a4dd0985567430f9ef18 FirstName First Name ... NaN TRUE
4 5e95a49b0985567430f8fc01 abc not_found ... NaN FALSE
5 5e95a4dd0985567430f9ef19 FirstNm First Name ... NaN TRUE
6 5e95a4dd0985567430f9ef20 LastName Last Name ... NaN TRUE
7 5e95a4dd0985567430f9ef21 LastNm Last Name ... NaN TRUE
8 5e95a49b0985567430f8fc02 LegalName Legal Name ... NaN TRUE
9 5e95a4dd0985567430f9ef22 LegalNm Legal Name ... NaN TRUE
10 5e95a4dd0985567430f9ef23 NickName Nick Name ... NaN TRUE
11 5e95a4dd0985567430f9ef24 pqr not_found ... NaN FALSE
12 5e95a49b0985567430f8fc03 NickNm Nick Name ... NaN TRUE
推荐阅读
- c - 为什么我的 C 程序虽然是一个简单的程序却没有被执行?
- python - 无法在 Linux 中安装 pandas
- python - 清理这个网络爬虫
- swift - SwiftUI,Xcode 12.4 在 Xcode 操场上的问题
- javascript - 不推荐使用事件层时,如何在传单 js 地图上绘制多边形?
- wikidata - 在 Wikidata 上创建具有特定类型值的声明
- php - 语法错误或访问冲突:1072 表中不存在键列“kategori_id”
- node.js - Firestore 子集合相当于 mongodb
- python - 如何在一列中对具有不同标准的 Pandas 数据框进行排序
- javascript - 拖动侧边栏以独立调整元素宽度,页面上任意数量的元素