0

With a DataFrame like the following:

temp = pd.DataFrame({'a':[1,4,7],'b':[2,5,8],'c':[3,6,9]}).T.rename(columns={0:'first_a',1:'first_b',2:'second'})

first_a first_b second
a   1   4   7
b   2   5   8
c   3   6   9

If we want to just subset and use first* columns and filter somehow like the following:

a = temp.filter(like='first').loc[(temp.filter(like='first') > 4).sum(axis=1) > 0, :]

first_a first_b
b   2   5
c   3   6

How can the rest of the information in the original DataFrame be retrieved for the matching samples to return the following:

first_a first_b second
b   2   5   8
c   3   6   9

I tried passing the matching indexes as you would do with a list of column headers but it does not work.

1 Answer 1

2

Just do not call filter outside the condition selection

temp.loc[(temp.filter(like='first') > 1).sum(axis=1) > 1, :]
Out[35]: 
   first_a  first_b  second
b        2        5       8
c        3        6       9
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.