pouchewar
pouchewar

Reputation: 409

Highest diff between max and min values in a pandas df

I have this df:

values = {'a':[1,2,3,4], 'b':[1,2,5,9], 'c':[10,1000,20,30]}
d=pd.DataFrame(values)

What's the best way to get the column with the highest spread between max and min values?

The output shoub be: c because 1000 - 10 > 9 - 1 > 4 - 1

Upvotes: 0

Views: 46

Answers (3)

Scott Boston
Scott Boston

Reputation: 153560

Try this:

d.columns[np.argmax(np.ptp(d, axis=0))]

Output:

'c'

Upvotes: 1

BoomBoxBoy
BoomBoxBoy

Reputation: 1885

A simple way of doing that is as follows

(d.max() - d.min()).idxmax()

Upvotes: 2

JANO
JANO

Reputation: 3076

Short and simple way:

d.apply(lambda x: max(x)-min(x)).idxmax()

Output:

c

Upvotes: 2

Related Questions