什么是Python中的“%”(在SQL中搜索单词的一部分)的等效项

时间:2019-06-28 09:05:50

标签: python dataframe

我希望在Python中相当于%(来自SQL)。

例如,如果需要查找所有内部带有“ a”的单词,则在SQL中,将“ WHERE”部分放在其中:

WHERE city LIKE '%a%'

所以我想做同样的事情,但要使用Python中的数据框。

2 个答案:

答案 0 :(得分:1)

import pandas as pd
df = pd.DataFrame("YOUR DATAFRAME")

# filter on dtype
test = df.select_dtypes([np.object]).apply(lambda x: x.str.contains('a').any())

答案 1 :(得分:0)

使用query,您可以使用SelectMany来翻译SQL col1.str.contains('a')

where col1 like '%a%'

通过df = pd.DataFrame({'col1':['dog', 'many', 'love', 'play'],'col2':['hh', 'many', 'xxxx', 'yyyy']}) df # Out: # col1 col2 # 0 dog hh # 1 many many # 2 love xxxx # 3 play yyyy # df.query("col1.str.contains('a')") # Out: # col1 col2 # 1 many many # 3 play yyyy ,您可以模仿startswith

where col1 like m%