pandas dataframe list部分字符串匹配python

时间:2016-06-16 15:59:05

标签: python list pandas dataframe

我有一个数据框df

df:

        chr          gene_name
0        1           ARF3
1        1           ABC
2        1           ARF3, ENSG123
3        1           ENSG,ARF3
4        1           ANG
5        2           XVY
6        2           PQR
7        3           RST
8        4           TAC 

和gene_list

gene_list = ['ARF3','ABC' ]

现在,我需要从数据框(df)获取行,其基因名称与gene_list中的元素完全匹配。

所以,我试过了:

df2 = df1[df.gene_name.isin(gene_list)]

我找回了:

       chr           gene_name
0        1           ARF3
1        1           ABC

但我期待的是:

        chr          gene_name
0        1           ARF3
1        1           ABC
2        1           ARF3, ENSG123
3        1           ENSG,ARF3

所以基本上数据框中的所有行,其中gene_list中的元素是数据框中gene_name的子字符串。

我想过使用.contains()如果我正在寻找另一种方式,数据框中的gene_name将成为gene_list中元素的子字符串。

所有帮助赞赏

1 个答案:

答案 0 :(得分:1)

join |所有值or gene_list = ['ARF3','ABC' ] print ('|'.join(gene_list)) ARF3|ABC print (df.gene_name.str.contains('|'.join(gene_list))) 0 True 1 True 2 True 3 True 4 False 5 False 6 False 7 False 8 False Name: gene_name, dtype: bool df2 = df[df.gene_name.str.contains('|'.join(gene_list))] print (df2) chr gene_name 0 1 ARF3 1 1 ABC 2 1 ARF3,ENSG123 3 1 ENSG,ARF3 可以使用contains

<resources>
<!--
TODO: Before you run your application, you need a Google Maps API key.
To get one, follow this link, follow the directions and press "Create" at the end:
https://console.developers.google.com/flows/enableapi?apiid=maps_android_backend&keyType=CLIENT_SIDE_ANDROID&r=FA:7B:BC:CE:B9:9E:4C:81:3D:91:35:B7:60:5E:9A:AA:B8:A5:B6:66%3Bcom.itshareplus.googlemapdemo
You can also add your credentials to an existing key, using this line:
FA:7B:BC:CE:B9:9E:4C:81:3D:91:35:B7:60:5E:9A:AA:B8:A5:B6:66;com.itshareplus.googlemapdemo
Alternatively, follow the directions here:
https://developers.google.com/maps/documentation/android/start#get-key
Once you have your key (it starts with "AIza"), replace the "google_maps_key"
string in this file.
-->
<string name="google_maps_key" templateMergeStrategy="preserve" translatable="false">YOUR_API_KEY</string>
</resources>