diedro
diedro

Reputation: 613

pandas select the minimum and max of each column and create a new dataframe

this is my dataframe:

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

I have just learned thank to you how to create a new dataframe selecting according to the min and max of a specific column. Thanks to @CHRD and @Quang Hoang.

I have just realize that this it not what I want. I would like to have a new dataframe with two row where in each column of the new dataframe has the min and max of each column of my dataframe. This is the expected result:

     A,B,C,D   
min  1,1,2,0
max 40,9,9,3

I have tried with this command but it seems to not work.

dfr_new = dfr[dfr.columns].min())

Upvotes: 0

Views: 1254

Answers (3)

Umar.H
Umar.H

Reputation: 23099

you can just call .agg on your entire dataframe.

df.agg(['min','max'])
      A  B  C  D   
min   1  1  2  0
max  40  9  9  3

Upvotes: 4

wwnde
wwnde

Reputation: 26676

df.describe().loc[['min','max']].astype(int)

    A   B   C   D
min 1   1   2   0
max 40  9   9   3

Upvotes: 1

XXavier
XXavier

Reputation: 1226

You can get that using this pd.DataFrame({'min': df.min(), 'max': df.max()}).T

    A   B   C   D
min 1   1   2   0
max 40  9   9   3

This is the dataframe i used.

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

Upvotes: 1

Related Questions