Skip to content
Advertisement

Select only available rows of a pandas dataframe

Let say I have the following pandas df

JavaScript

Now I have another array

JavaScript

Clearly, the element 'x' is not available in my original df. How can I select rows of df based on select but choose only available rows without any error? i.e. in this case, I want to select only rows corresponding to 'c' and 'a' maintaining this order.

Any pointer will be very helpful.

Advertisement

Answer

You could use reindex + dropna:

JavaScript

you could also filter select before reindex:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement