如何在pandas中求列表和数据框的交集?

3

我有一个列表,看起来像这样:

set(['loc. 08652', 'loc. 14331', 'loc. 08650', 'loc.06045', 'loc.10160', 'loc. 08656']

I have a data frame that looks like this:

                             lung     heart      kidney 
asx1.1_ox1.0.loc.08652  32.406993  51.709692  15.883315  
asx1.1_ox1.0.loc.14331   5.255465  86.048540   8.695995  
asx1.1_ox1.0.loc.12124  34.730648  39.070967  26.198384 
asx1.1_ox1.0.loc.06045  50.992902  28.701922  20.305177 
asx1.1_ox1.0.loc.10160  27.619962  63.702141   8.677896  
asx1.1_ox1.0.loc.20210  45.148668  43.700587  11.150744 

如何方便地生成两个文件的交集,并输出像下面那样的数据框:

                             lung     heart      kidney 
asx1.1_ox1.0.loc.08652  32.406993  51.709692  15.883315  
asx1.1_ox1.0.loc.14331   5.255465  86.048540   8.695995  
asx1.1_ox1.0.loc.06045  50.992902  28.701922  20.305177 
asx1.1_ox1.0.loc.10160  27.619962  63.702141   8.677896  

2
请查看pandas.DataFrame.isin。你基本上要做的是:在强制将set中的元素与数据框索引的结构匹配后,执行new_df = df[df.index.isin(my_set_of_locators)] - blacksite
1个回答

5
您可以清理索引,即删除loc之前的额外字符串,然后按照@not_a_robot的建议使用isin方法:
s = set(['loc.08652', 'loc.14331', 'loc.08650', 'loc.06045', 'loc.10160', 'loc. 08656']
# the set has been cleaned here so that it doesn't contain spaces

df[df.index.str.replace(".*(?=loc)", "").isin(s)]

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接