Pandas text matching like SQL's LIKE?

和自甴很熟 提交于 2019-12-20 16:34:20

问题


Is there a way to do something similar to SQL's LIKE syntax on a pandas text DataFrame column, such that it returns a list of indices, or a list of booleans that can be used for indexing the dataframe? For example, I would like to be able to match all rows where the column starts with 'prefix_', similar to WHERE <col> LIKE prefix_% in SQL.


回答1:


You can use the Series method str.startswith (which takes a regex):

In [11]: s = pd.Series(['aa', 'ab', 'ca', np.nan])

In [12]: s.str.startswith('a', na=False)
Out[12]: 
0     True
1     True
2    False
3    False
dtype: bool

You can also do the same with str.contains (using a regex):

In [13]: s.str.contains('^a', na=False)
Out[13]: 
0     True
1     True
2    False
3    False
dtype: bool

So you can do df[col].str.startswith...

See also the SQL comparison section of the docs.

Note: (as pointed out by OP) by default NaNs will propagate (and hence cause an indexing error if you want to use the result as a boolean mask), we use this flag to say that NaN should map to False.

In [14]: s.str.startswith('a')  # can't use as boolean mask
Out[14]:
0     True
1     True
2    False
3      NaN
dtype: object



回答2:


you can use

s.str.contains('a', case = False)



回答3:


  1. To find all the values from the series that starts with a pattern "s":

SQL - WHERE column_name LIKE 's%'
Python - column_name.str.startswith('s')

  1. To find all the values from the series that ends with a pattern "s":

SQL - WHERE column_name LIKE '%s'
Python - column_name.str.endswith('s')

  1. To find all the values from the series that contains pattern "s":

SQL - WHERE column_name LIKE '%s%'
Python - column_name.str.contains('s')

For more options, check : https://pandas.pydata.org/pandas-docs/stable/reference/series.html



来源:https://stackoverflow.com/questions/22291565/pandas-text-matching-like-sqls-like

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!