-1

I'm relatively green to python and have mainly been using pandas for data analysis.

I have a DataFrame with 2 columns, A and B, and then a list of values called C. I need my code to check whether each value in df.B is in list C, and for all that are True then return the corresponding values from df.A to a list.

I've tried using a for loop, and later found the .isin() function but just can't figure out how to utilize either of these in a way that'll give me the result I need. I'd really appreciate any help or hints!

buhtz
  • 8,057
  • 11
  • 59
  • 115
  • 3
    Please share the corresponding, simplified code on that issue. Also see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) – QuagTeX Jul 26 '21 at 12:55
  • 1
    Please provide an example of your data and the expected output. A visual example is much better than a lengthy description of your data. – mozway Jul 26 '21 at 12:56
  • 1
    [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/4865723) will help you, too. – buhtz Jul 26 '21 at 13:03

2 Answers2

2

Use:

df.loc[df['B'].isin(C), 'A'].tolist()
ansev
  • 28,746
  • 5
  • 11
  • 29
1

Never use a loop when you can select things natively with pandas.

df[df.B.isin(C)].A.to_list()

If the B name is not a single word (e.g. COLUMN NAME), use:

df[df['B'].isin(C)]['A'].to_list()

df.B.isin(C) will return a Series of True/False depending on whether the values in B are in C. Then use this to select the rows in the original dataframe. Finally, select the column A and convert to list.

buhtz
  • 8,057
  • 11
  • 59
  • 115
mozway
  • 81,317
  • 8
  • 19
  • 49
  • 1
    I don't use slicing by attribute. Not just when a column name contains whitespace, but there may be name collisions with names like 'max' and such; some of which may occur in future versions of the DataFrame class. So my code would break when they add a new attribute. Using `loc`, `iloc` and the regular python `[]` slicing syntax doesn't have that problem. – Chris Wesseling Jul 26 '21 at 13:15
  • I fully agree, I actually always use `[]` in my own code. – mozway Jul 29 '21 at 09:10