[英]Searching a filtered dataframe for a specific string and then creating a new column based on the results (Python/Pandas)
[英]Pandas (Python Module) - Searching for String in Specific Column
很抱歉,如果這是一個基本問題,我今天就開始使用pandas模塊了。 但基本上我用它來清理我正在使用的這個csv文件。 我正在嘗試在第二列中搜索特定的字符串(或子字符串),如果我找到它,我想在數據集中添加一個新列,該列將包含布爾值true / false(如果我找到了是否給定字符串)。 建議?
您可以使用Series.str.contains()方法:
df['new'] = df.iloc[:, 1].str.contains(r'substring', flags=re.I)
演示:
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.