Python | Pandas Series.str.contains() 过滤pandas datafram格式中包含特定字符串的行
Example #1: Use Series.str.contains a () function to find if a pattern is present in the strings of the underlying data in the given series object.
- Python3
# importing pandas as pd import pandas as pd # importing re for regular expressions import re # Creating the Series sr = pd.Series([ 'New_York' , 'Lisbon' , 'Tokyo' , 'Paris' , 'Munich' ]) # Creating the index idx = [ 'City 1' , 'City 2' , 'City 3' , 'City 4' , 'City 5' ] # set the index sr.index = idx # Print the series print (sr) |
Output :
Now we will use Series.str.contains a () function to find if a pattern is contained in the string present in the underlying data of the given series object.
- Python3
# find if 'is' substring is present result = sr. str .contains(pat = 'is' ) # print the result print (result) |
Output :
As we can see in the output, the Series.str.contains() function has returned a series object of boolean values. It is true if the passed pattern is present in the string else False is returned.
Example #2: Use Series.str.contains a () function to find if a pattern is present in the strings of the underlying data in the given series object. Use regular expressions to find patterns in the strings.
- Python3
# importing pandas as pd import pandas as pd # importing re for regular expressions import re # Creating the Series sr = pd.Series([ 'Mike' , 'Alessa' , 'Nick' , 'Kim' , 'Britney' ]) # Creating the index idx = [ 'Name 1' , 'Name 2' , 'Name 3' , 'Name 4' , 'Name 5' ] # set the index sr.index = idx # Print the series print (sr) |
Output :
Now we will use Series.str.contains a () function to find if a pattern is contained in the string present in the underlying data of the given series object.
- Python3
# find if there is a substring such that it has # the letter 'i' followed by any small alphabet. result = sr. str .contains(pat = 'i[a-z]' , regex = True ) # print the result print (result) |
Output :
As we can see in the output, the Series.str.contains() function has returned a series object of boolean values. It is true if the passed pattern is present in the string else False is returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import pandas as pd import csv aliUid = [ "123" , "124" , "125" , "126" ] file = './123.log' data = pd.read_csv( file ,delimiter = ',' ,quoting = csv.QUOTE_NONE,header = None ) for uid in aliUid: df = data.loc[data[ 52 ]. str .contains(uid)] for column in df: df[column] = df[column]. str .replace( '"' ,'') print (df) new_file = f "./{uid}.log" df.to_csv(new_file,quoting = csv.QUOTE_NONE,index = False ,header = False ) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
2018-06-20 阿里云dataworks数据工场用户使用子账号
2018-06-20 NodeJS on Nginx: 使用nginx反向代理处理静态页面