[英]Find substrings that match a list
How do I find all the substrings matching a df
column value?如何找到与
df
列值匹配的所有子字符串?
text = "The quick brown fox jumps over the lazy dog"
df = pd.DataFrame(['quick brown fox', 'jump', 'lazy dog', 'banana', 'quick fox'], columns=['value'])
results = get_matches(df, text)
# Excepted results: ['quick brown fox', 'jump', 'lazy dog']
One option:一种选择:
import pandas as pd
text = "The quick brown fox jumps over the lazy dog"
df = pd.DataFrame(['quick brown fox', 'jump', 'lazy dog', 'banana', 'quick fox'], columns=['value'])
def get_matches(df, text):
return df[df['value'].apply(text.__contains__)]
res = get_matches(df, text)
print(res)
Output Output
value
0 quick brown fox
1 jump
2 lazy dog
As an alternative, use str.find :作为替代方案,使用str.find :
def get_matches(df, text):
return df[df['value'].apply(text.find).ne(-1)]
res = get_matches(df, text)
print(res)
Output Output
value
0 quick brown fox
1 jump
2 lazy dog
Try:尝试:
def get_matches(df, text):
return df.loc[[t in text for t in df['value']], 'value']
get_matches(df, text)
Output: Output:
0 quick brown fox
1 jump
2 lazy dog
Name: value, dtype: object
List=[]
for a in df.value:
if a in text:
print(a)
List.append(a)
print(List)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.