首页 > 解决方案 > 查找与列表匹配的子字符串

问题描述

如何找到与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']

标签: pythonpandas

解决方案


一种选择:

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)

输出

             value
0  quick brown fox
1             jump
2         lazy dog

作为替代方案,使用str.find

def get_matches(df, text):
    return df[df['value'].apply(text.find).ne(-1)]


res = get_matches(df, text)
print(res)

输出

             value
0  quick brown fox
1             jump
2         lazy dog

推荐阅读