如何在过滤数据时结合使用区分大小写的Isin和contains

时间:2019-05-16 16:13:45

标签: python python-3.x pandas pandas-groupby

我有一个数据框,并且我有多个条件可以过滤该数据 例如:

state =  Andhra Pradesh, Uttar Pradesh, Madya Pradesh, Karnataka, Telangana 

field_id = 23234,23235,34342,34343...

 Input: 
 --------
field_id,state,latitude,longitude
23234,Uttar Pradesh,28.05990445,78.34249247
23235,Uttar Pradesh,27.6206712,78.7132515
23236,Uttar Pradesh,27.95378227,79.04653615
23237,Uttar Pradesh,28.63232205,78.5763061
34342,Telangana,18.71517059,78.70715893
34343,Telangana,18.11666882,79.3873906
34344,Telangana,18.4527501,79.21027678
45452,Madhya Pradesh,21.70770808,75.67139279
45453,Madhya Pradesh,21.75613982,76.29656126
45454,Madhya Pradesh,22.05077277,79.2375098
56562,Karnataka,12.38318394,76.02965431
56563,Karnataka,12.38297092,76.02957524
56564,Karnataka,14.26369026,75.43180631
67672,Andhra Pradesh,16.83612945,80.99260486
67673,Andhra Pradesh,16.83277887,80.99537748
67674,Andhra Pradesh,16.83311322,80.99560818

我尝试了

state = Andhra Pradesh,Madhya Pradesh,Karnataka
State1 = State.split(',')
dsna = dsna.loc[dsna['State'].isin(State1)]

使用完全匹配的大小写

field_id = 23234,34345
field_id1 = field_id.split(',')
dsna = dsna.loc[dsna['field_id'].isin(field_id1)]

这里还过滤确切的数据帧

此处dsna是我的数据框,我将附加该数据框或表的CSV

dsna = dsna.loc[dsna['field_id'].isin(field_id1)]
dsna = dsna.loc[dsna['State'].isin(State1)]

我想要基于正则表达式格式的数据过滤 像状态=以色列,telangana(我给出了小写字母) 它应该过滤与这些状态有关的所有数据,并列出经度和纬度 field_id= 232,343,454的相似方式应过滤所有与数据相关的字段,期望数据应与正则表达式和区分大小写有关,并过滤多个值,任何熊猫功能都可以做到这一点,因为我的数据已经在数据框中了

1 个答案:

答案 0 :(得分:0)

通过使用熊猫

dsna = dsna[dsna['State'].str.contains('|'.join(State),
                                                     case=False)]

我使用|搜索整个字符串。