Eduardo EPF
Eduardo EPF

Reputation: 170

Using DataFrame.ISIN() with a series made of series

I am trying to filter the rows of a Dataframe depending if the value of a column is part of a series so:

pack_data_clean=pack_data_clean[pack_data_clean["actual_box_barcode"].isin(ref_list)==True]

Where pack_data_clean is the dataframe to be filtered, actual_box_barcode the column to check and ref_list the series with the values that will remain in the dataframe.

However ref_list is not a regular series with values, but a series made of series, let's see:

ref_list=["BG1", "BG2", "BG3", "BG4", A1_refs, A2_refs, A3_refs, C1_refs, C2_refs, C3_refs, C4_refs, C5_refs
    , E0_refs, E1_refs, E3_refs, E4_refs, E6_refs, E7_refs, E36_refs]

Where A1_refs e.g. is:

A1_refs=["EEC", "ENC", "EZC"]

Right now my code is only filtering the rows that are "BG1", "BG2", "BG3", "BG4" but I would like to filter as well "EEC", "ENC", "EZC" and so on.

Could you please help me to ¡create this filter accordingly? Thanks, Eduardo

Upvotes: 0

Views: 35

Answers (1)

Ashish Acharya
Ashish Acharya

Reputation: 3399

If it's a list of regular objects and series, you need to get it into a consistent series format. This might help:

ref_list = pd.Series(i for i in my_list if type(i) != pd.Series).append(list(j for j in my_list if type(j) == pd.Series))

If it's already a Series of Series, try flattening the series before using it, like so:

ref_list = ref_list.ravel()

Upvotes: 1

Related Questions