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

Time:05-29

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

enter image description here

CodePudding user response:

root_project_id = 'd6d4e77e-b8ec-467a-ba06-1c6079aa2d82'
df.query("root_project_id in PathDSC")

CodePudding user response:

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]
        # ...
  • Related