0

here is the case need to ask your advice.

I have a dataframe as below by using line

df = pd.DataFrame(np.arange(1,400,10).reshape(10, 4), columns=['A', 'B', 'C', 'D'])
df

enter image description here

Then, I have a list as following:

array([ 331,  51, 131, 211])

I would like to get a table with column B only contains the values stated in the list. For instance, I generate the expected output manually as below:

enter image description here

Thank you for your advice in advance. Thanks!

Yeo Keat
  • 143
  • 1
  • 9

1 Answers1

0

I think you can do it with the isin function of pandas https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.isin.html

df[df["B"].isin(yourarray.tolist())]
Renaud
  • 2,709
  • 2
  • 9
  • 24
  • Thank you for your help! Your answer has helped me! I just realize it can be done with just a single line, that is awesome! – Yeo Keat Apr 26 '20 at 20:35