3

Input:

pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd']) 

Desired result:

['a', 'c'] # can be in pd.Series or np.array format, doesn't matter. 

But I want a solution which does not store the series in a temporary variable, e.g.

s = pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd']) s[s == True].index 

stores the series in s, and uses it twice.

The solution I'm looking for is like

np.where(s) 

But returns the label of the true values, rather than their integer indices.

Notes:

This question is similar to this one, but more specific.

1 Answer 1

4

I always pass to loc

pd.Series([True, False, True, False], index=['a', 'b', 'c', 'd']).loc[lambda x : x].index Index(['a', 'c'], dtype='object') 
Sign up to request clarification or add additional context in comments.

2 Comments

Aha! Great answer. So there's no builtin for this in pandas?
@AlexLenail I do not think so ~

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.