0

I am trying to do something pretty simple in Pandas but having trouble figuring out the cleanest way to do it.

I have a dataframe with a list of entities - they have names, IDs, prices, and other descriptive information. An example is below.

I would like to be able to first filter this dataframe based on a selection of a subset of IDs. In other words, if X=0, Y=1, and Z=1, I would like to only include entries with IDs Y or Z. Therefore, my output would only have the Jerry and Ben rows in the output.

I'm trying this form but am having trouble linking the binary variables for each ID to the string characters.

Any suggestions? Thank you for your help.

'''User input'''
X=0
Y=0
Z=0

ID_list=[X,Y,Z]

for ID in ID_list:
    for entity in so_df:
        if ID_list[i]=1 and so_df.iloc[entity]['ID']=???

enter image description here

Z_D
  • 797
  • 2
  • 12
  • 30
  • 3
    Are there only 3 types of IDs? Why not ask the user to just input the id itself? This notion of binary seems pointless. – cs95 Aug 20 '18 at 16:14
  • Definitely agree w/ @coldspeed here. If you are simply checking for specific values in a column, there is no need to convert them to binary. You are just adding unnecessary overhead. Try `df[df['ID'].isin(['Y','Z'])]` – rahlf23 Aug 20 '18 at 16:16
  • Thank you. Point of binary is that other times it would not be only Y or Z. For instance, it would be X only, or X and Z. – Z_D Aug 20 '18 at 16:25
  • 1
    @Z_D binary is not necessary just call your input function with split() and they can enter as many IDs separated by a space as they want: `input_value = input('ID: ').split()` then just filter as @rahlf23 said: `df[df['ID'].isin(input_value)]` – It_is_Chris Aug 20 '18 at 16:29

0 Answers0