diedro
diedro

Reputation: 613

selecting rows with min and max values of a defined column in pandas

I have the following dataframe:

A,B,C,D   
10,1,2,3
 1,4,7,3
10,5,2,3
40,7,9,3
9,9,9,9

I would like to create another dataframe starting from the previous one which have only two row. The selection of these two rows is based on the minimum and maximum value in the column "A". I would like to get:

A,B,C,D   
 1,4,7,3
40,7,9,3

Do you think I should work with a sort of index.min e index.max and then select only the two rows and append then in a new dataframe? Do you have same other suggestions?

Thanks for any kind of help, Best

Upvotes: 0

Views: 1086

Answers (2)

CHRD
CHRD

Reputation: 1957

IIUC you can simply subset the dataframe with an OR condition on df.A.min() and df.A.max():

df = df[(df.A==df.A.min())|(df.A==df.A.max())]

df
    A   B   C   D
1   1   4   7   3
3   40  7   9   3

Upvotes: 5

Quang Hoang
Quang Hoang

Reputation: 150745

Yes, you can use idxmin/idxmax and then use loc:

df.loc[df['A'].agg(['idxmin','idxmax']) ]

Output:

    A  B  C  D   
1   1  4  7  3
3  40  7  9  3

Note that this only gives one row for min and one for max. If you want all values, you should use @CHRD's solution.

Upvotes: 1

Related Questions