我有一个Dataframe
df = pd.DataFrame({'content':['A Name:[MINSDDL], Code:[KRKN]','A Name:[LAVRION]','C:[ANGELA]','C:[KUMASI]']})
我想要用正则表达式的方法 提取Code:[] ; A Name:[] ; C:[] 里面的值
结果像这样子
请各位帮忙
我有一个Dataframe
df = pd.DataFrame({'content':['A Name:[MINSDDL], Code:[KRKN]','A Name:[LAVRION]','C:[ANGELA]','C:[KUMASI]']})
我想要用正则表达式的方法 提取Code:[] ; A Name:[] ; C:[] 里面的值
结果像这样子
import pandas as pd
import re
def fun(se):
r1 = re.findall(r'Code:\[(.*?)\]', se['content'])
if r1 == []:
r1 = ['']
r2 = re.findall(r'A Name:\[(.*?)\]', se['content'])
if r2 == []:
r2 = ['']
r3 = re.findall(r'C:\[(.*?)\]', se['content'])
if r3 == []:
r3 = ['']
return r1[0], r2[0], r3[0]
df = pd.DataFrame({'content':['A Name:[MINSDDL], Code:[KRKN]','A Name:[LAVRION]','C:[ANGELA]','C:[KUMASI]']})
print(df)
df[['B', 'C', 'D']] = df.apply(fun, axis = 1, result_type='expand')
print(df)
'''--result
content
0 A Name:[MINSDDL], Code:[KRKN]
1 A Name:[LAVRION]
2 C:[ANGELA]
3 C:[KUMASI]
content B C D
0 A Name:[MINSDDL], Code:[KRKN] KRKN MINSDDL
1 A Name:[LAVRION] LAVRION
2 C:[ANGELA] ANGELA
3 C:[KUMASI] KUMASI
'''