我需要从一个列中的字符串中提取所有匹配项,并填充第二个列。匹配项将会用逗号分隔。
期望在列中输出
df2 = pd.DataFrame([[1000, 'Jerry', 'string of text BR1001_BR1003_BR9009 more string','BR1003',''],
[1001, '', 'BR1010_BR1011 random text', 'BR1010',''],
['', '', 'test to discardBR3009', 'BR2002',''],
[1003, 'Perry','BR4009 pure gibberish','BR1001',''],
[1004, 'Perry2','','BR1001','']],
columns=['ID', 'Name', 'REGEX string', 'Member of','Status'])
表示要提取的代码的模式。
BR_pat = re.compile(r'(BR[0-9]{4})', re.IGNORECASE)
期望在列中输出
BR1001, BR1003, BR9009
BR1010,BR1011
BR3009
BR4009
My attempt:
df2['REGEX string'].str.extractall(BR_pat).unstack().fillna('').apply(lambda x: ", ".join(x))
输出:
match
0 0 BR1001, BR1010, BR3009, BR4009
1 BR1003, BR1011, ,
2 BR9009, , ,
多余的逗号和缺少行,我做错了什么?