创建DataFrame
import pandas as pd
df=pd.DataFrame(['zero','one','two','three','four','five'],columns=['num'])
df
num
0 zero
1 one
2 two
3 three
4 four
5 five
1、以XXX开头
# 方法1:startswith函数
df[df.num.str.startswith('o')]
# 方法2:contains函数+正则表达式
df[df.num.str.contains('^o')]
# 方法3:query函数
df.query("num.str.startswith('o')",engine='python')
num
1 one
2、以XXX结尾
# 方法1:endswith函数
df[df.num.str.endswith('o')]
# 方法2:contains函数+正则表达式
df[df.num.str.contains('o$')]
# 方法3:query函数
df.query("num.str.endswith('o')",engine='python')
num
0 zero
2 two
3、包含XXX的
# 方法1:contains函数+正则表达式
df[df.num.str.contains('o')]
# 方法2:query函数
df.query("num.str.contains('o')",engine='python')
num
0 zero
1 one
2 two
4 four