The Nightman
The Nightman

Reputation: 5769

Use index values to select subset of pandas dataframe

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.

Upvotes: 0

Views: 578

Answers (1)

BENY
BENY

Reputation: 323396

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

Upvotes: 2

Related Questions