以下是pandas
data frame
我拥有的
index name_matches dist_matches
38 PO1000000345 M-00346 M-00346
39 PO1000000352 M-00804
40 PO1000000354 M-00196 M-00196
41 PO1000000355 M-00514 M-00514
42 PO1000000382 M-00353,M-00354 M-00354
43 PO1000000411
44 PO1000000451
45 PO1000000512 M-00680
46 PO1000000530 M-00089
47 PO1000000531 M-00087 M-00087
48 PO1000000553 M-00917,M-00920,M-00922 M-00920
我正在尝试获取一个新的列(comb_matches
),该列会提取出name_matches
和dist_matches
列中的匹配值。有时,列中会有一个或多个用逗号分隔的值。我想要获取的输出示例如下所示。
index name_matches dist_matches comb_matches
38 PO1000000345 M-00346 M-00346 M-00346
39 PO1000000352 M-00804
40 PO1000000354 M-00196 M-00196 M-00196
41 PO1000000355 M-00514 M-00514 M-00514
42 PO1000000382 M-00353,M-00354 M-00354 M-00354
43 PO1000000411
44 PO1000000451
45 PO1000000512 M-00680
46 PO1000000530 M-00089
47 PO1000000531 M-00087 M-00087 M-00087
48 PO1000000553 M-00917,M-00920,M-00922 M-00920 M-00920
有没有简单的方法来获得以上?
使用str.split
之前isin
。然后我们将布尔值实现为np.where
Bool=df.name_matches.str.split(',',expand=True).isin(df.dist_matches).any(1)
df['comb_match']=np.where(Bool,df.dist_matches,'')
df
Out[520]:
index name_matches dist_matches comb_match
38 PO1000000345 M-00346 M-00346 M-00346
39 PO1000000352 M-00804
40 PO1000000354 M-00196 M-00196 M-00196
41 PO1000000355 M-00514 M-00514 M-00514
42 PO1000000382 M-00353,M-00354 M-00354 M-00354
43 PO1000000411
44 PO1000000451
45 PO1000000512 M-00680
46 PO1000000530 M-00089
47 PO1000000531 M-00087 M-00087 M-00087
48 PO1000000553 M-00917,M-00920,M-00922 M-00920 M-00920
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句