[英]Pandas (Python Module) - Searching for String in Specific Column
Sorry if this is a basic question, I just started using the pandas module today. 很抱歉,如果这是一个基本问题,我今天就开始使用pandas模块了。 But basically I'm using it to clean up this csv file I'm working with. 但基本上我用它来清理我正在使用的这个csv文件。 I'm trying to search for a specific string (or substring) in the second column and if I find it I wanted to add a new column to the dataset that will either contain a boolean value of true/false (for if I found the given string or not). 我正在尝试在第二列中搜索特定的字符串(或子字符串),如果我找到它,我想在数据集中添加一个新列,该列将包含布尔值true / false(如果我找到了是否给定字符串)。 Suggestions? 建议?
You can use Series.str.contains() method: 您可以使用Series.str.contains()方法:
df['new'] = df.iloc[:, 1].str.contains(r'substring', flags=re.I)
Demo: 演示:
In [40]: import re
In [41]: df
Out[41]:
a b c
0 1 Anna 10
1 2 Barton 11
2 3 Max 12
In [42]: df['new'] = df.iloc[:, 1].str.contains(r'ma', flags=re.I)
In [43]: df
Out[43]:
a b c new
0 1 Anna 10 False
1 2 Barton 11 False
2 3 Max 12 True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.