Python 将dataframe列转换为包含正则表达式模式的字典
我有以下数据框:Python 将dataframe列转换为包含正则表达式模式的字典,python,pandas,performance,dictionary,Python,Pandas,Performance,Dictionary,我有以下数据框: dct = {"Keyword":("pinpad",'printer','mom','got','phone'), "Pattern":("pin pad, pinpad","print, cannon",'mom','got','phone, ipad') } df = pd.DataFrame(dct) print(df) Keyword P
dct = {"Keyword":("pinpad",'printer','mom','got','phone'),
"Pattern":("pin pad, pinpad","print, cannon",'mom','got','phone, ipad')
}
df = pd.DataFrame(dct)
print(df)
Keyword Pattern
0 pinpad pin pad, pinpad
1 printer print, cannon
2 mom mom
3 got got
4 phone phone, ipad
我想转换成dict,一列是键,另一列是值。
需要进行以下更改:
1.对于逗号(,)分隔的多个元素,应使用“|”进行转换
2.单词之间的空格应替换为(\s+),以便在正则表达式模式匹配中使用。
3.并且它应该具有预期输出中提到的前缀和后缀:
预期口述:
keyword_dict = {
'pinpad': re.compile(r'.*(pin\s+pad|pinpad).*'),
'printer': re.compile(r'.*(print|cannon).*'),
'mom': re.compile(r'.*(mom).*'),
'got': re.compile(r'.*(got).*'),
'phone': re.compile(r'.*(phone|ipad).*')
}
我迄今为止所做的工作:
df['Pattern'] = df['Pattern'].replace(',\s+','|', regex= True)
df['Pattern'] = df['Pattern'].str.replace(' ',"\s+", regex = True)
df['Pattern'] = "re.compile(r'.*(" + df['Pattern'].astype(str) + ").*')"
keyword_dict = dict(zip(df.Keyword, df.Pattern))
我想知道是否有更简洁、更专业的方式将整个转换过程转化为dict?
谢谢,如果需要更多信息,请告诉我
更新:
预期输出已被标记,并且添加了“到目前为止我所做的工作”部分。您能给出您对本示例预期的确切输出吗