我正在努力清洁pandas DataFrame列。该列中有我想从列表中查找和提取的单词。
下面是我所拥有的。但是它不会返回多个匹配项。下面是一个示例。
data = {'A':['abc 1 foo','def 1,bar','abc 2','def 2', 'abc 1/def 1 baz', 'abc 1,def 1']}
l = ['abc 1', 'def 1']
df = pd.DataFrame(data)
for idx, row in df.iterrows():
for x in l:
if x in row.A:
df.loc[idx, 'new_col'] = x```
Actual output:
A new_col
abc 1 abc 1
def 1 def 1
abc 2 NaN
def 2 NaN
abc 1/def 1 def 1
abc 1,def 1 def 1
Expected output:
A new_col
abc 1 abc 1
def 1 def 1
abc 2 NaN
def 2 NaN
abc 1/def 1 abc 1,def 1
abc 1,def 1 abc 1,def 1
Note: the seperator in col A could be anything('/', ';') but seperator in new_col should be fixed.
答案 0 :(得分:1)
将str.findall
与Series.str.join
结合使用list的值,其中模式由|
表示正则表达式OR
,而\b
表示单词边界:
pat = '|'.join(r"\b{}\b".format(x) for x in l)
df['new_col'] = df['A'].str.findall(pat).str.join(',')
print (df)
A new_col
0 abc 1 foo abc 1
1 def 1,bar def 1
2 abc 2
3 def 2
4 abc 1/def 1 baz abc 1,def 1
5 abc 1,def 1 abc 1,def 1
如果需要NaN
而不是空字符串,请使用numpy.where
:
pat = '|'.join(r"\b{}\b".format(x) for x in l)
s = df['A'].str.findall(pat)
df['new_col'] = np.where(s.astype(bool), s.str.join(','), np.nan)
print (df)
A new_col
0 abc 1 foo abc 1
1 def 1,bar def 1
2 abc 2 NaN
3 def 2 NaN
4 abc 1/def 1 baz abc 1,def 1
5 abc 1,def 1 abc 1,def 1