返回数据框形式的行,该行在特定列中包含一些字

2024-06-16 11:19:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望在包含多个搜索条件的数据帧中获得这样一行。 例如 路径描述 他是个好孩子 拉宾是个好孩子 他很好 他是个男孩

我希望以数据帧的形式获取所有行,其中包含('good','boy')) retun应该是 他是个好孩子 拉宾是个好孩子


Tags: 数据路径条件形式goodboy男孩retun
2条回答

我为你做这个。。。没有测试

def find_intrastr_column_df(df, column_name, list_of_str_to_find):
    """
    Loop in a pandas dataframe, trying to find all rows 
    that contains the listed words in a particular column and 
    return a selected dataframe only with the rows founded.

    df: Pandas Dataframe
    column_name: String. Columns name
    list_of_str_to_find: List. List of String of words to find.
    """

    list_rows_find = []

    for index, row in df.iterrows():
        cell_value = row[column_name]
        # ignore nan values
        if cell_value != cell_value:
            break
        else:
            pass

        found = True
        for str in list_of_str_to_find:
            if str not in cell_value:
                found = False
                break
            else:
                pass

        if found:
            list_rows_find.append(index)
        else:
            pass

    df_selected = df.loc[list_rows_find,:]
    return df_selected

让我们一步一步走

认为您的数据框如下:

import pandas as pd
df = pd.DataFrame({
    'path': ['c:/data', 'd:/data', 'e:/data', 'f:/data'],
    'description': ['he is a good boy', 'rabin is a good boy', 'he is good', 'he is a boy']
})
df
    description         path
0   he is a good boy    c:/data
1   rabin is a good boy d:/data
2   he is good          e:/data
3   he is a boy         f:/data

这就是你得到你想要的东西的方式:

df[df['description'].str.contains('good') & df['description'].str.contains('boy')]
    description         path
0   he is a good boy    c:/data
1   rabin is a good boy d:/data

相关问题 更多 >