我有一个像这样的数据库:
df['col'] = ['text', 'Texts', 'Text-Pro', 'Text;Nothing', 'Pro', 'pro', 'Pros', 'Nothing']我想要做的是迭代,并找到这些值,但它不起作用:
for i in range(len(df['col']):
if df.loc[i, 'col'] in list(['text', 'pro']):
print('yes')我想要的输出:
yes
yes
yes
yes
yes
yes
yes发布于 2021-06-04 02:09:57
下面的答案应该是你所要求的。请注意,为了更好地使用您的DataFrame,我重新构造了一些项。(根据你的代码,我想你是和熊猫一起工作的)。
import pandas
# outsource the matching to a function so we can use `map` below
def match_string(input_string):
"""
"""
target_strings = ['text', 'pro']
for target in target_strings:
# you need to lower your strings if you want the match to be case-insensitive
if target in input_string.lower():
print("yes")
return "yes"
print("no")
return "no"
df = pandas.DataFrame()
df['col'] = ['text', 'Texts', 'Text-Pro', 'Text;Nothing', 'Pro', 'pro', 'Pros', 'Nothing']
# instead of iterating, use a map operation which is much more efficient
# this will store the result in the column 'string_matched' as well as print it out as the question requests
df["string_matched"] = df["col"].map(lambda x: match_string(x))你可以找到一些关于熊猫地图这里的文档。
输出
yes
yes
yes
yes
yes
yes
yes
no发布于 2021-06-04 02:09:35
这就是你要找的吗?基本上,您需要检查列中的'text'或'pro'。因此,您应该将这两个字符串与列进行比较。
import pandas as pd
df=pd.DataFrame()
df['col'] = ['text', 'Texts', 'Text-Pro', 'Text;Nothing', 'Pro', 'pro', 'Pros', 'Nothing']
list1=[]
for i in range(len(df['col'])):
if 'text' in str(df['col'][i]).lower() or 'pro' in str(df['col'][i]).lower():
list1.append("yes")
else:
list1.append("no")
df['Test/Pro']=list1
print(df)输出:
col Test/Pro
0 text yes
1 Texts yes
2 Text-Pro yes
3 Text;Nothing yes
4 Pro yes
5 pro yes
6 Pros yes
7 Nothing no发布于 2021-06-04 02:09:56
您可以使用regex和布尔掩蔽。您可能需要更新regex以匹配您的用例。
import pandas as pd
df = pd.DataFrame()
df['col'] = ['text', 'Texts', 'Text-Pro', 'Text;Nothing', 'Pro', 'pro', 'Pros', 'Nothing']
masking = df['col'].str.contains(r'(Text|text)|(Pro|pro)')
print(masking)版画
0 True
1 True
2 True
3 True
4 True
5 True
6 True
7 False
Name: col, dtype: boolhttps://stackoverflow.com/questions/67830549
复制相似问题