简体   繁体   中英

how to check value existing in pandas dataframe column value of type list

I have pandas dataframe which contains value in below format. How to filter dataframe which matches the 'd6d4e77e-b8ec-467a-ba06-1c6079aa2d82' in any of the value of type list part of PathDSC column

i tried

def get_projects_belongs_to_root_project(project_df, root_project_id):
        filter_project_df = project_df.query("root_project_id in PathDSC")

it didn't work i got empty dataframe

在此处输入图像描述

Assuming the values of PathDSC column are lists of strings, you can check row-wise if each list contains the wanted value and mask those rows using Series.apply . Then select only those rows using boolean indexing.

def get_projects_belongs_to_root_project(project_df, root_project_id):
        mask = project_df['PathDSC'].apply(lambda lst: root_project_id in lst)
        filter_project_df = project_df[mask]
        # ...
root_project_id = 'd6d4e77e-b8ec-467a-ba06-1c6079aa2d82'
df = df[df['PathDSC'].str.contains(root_project_id)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM