1

I am having a long dataset and would like to get min/max values in a row:

    County   Year1   Year2   Year3   Year4
1   Autauga  54660   55253   51253   56253       

Output should look like

    County   Year1   Year2   Year3   Year4   Min     Max    Max-Min
1   Autauga  54660   55253   51253   56253   51253   56253  5000

My first shot yielded a string as max value (I've read about all the reasons on the forum):

df['Max'] = df.max(axis=1)

1) How can I exclude my first column so the max function runs correctly (I still need the county in my output)?

2) How can I run the max,min functions AND calc the difference of each value in one go?

Cheers, P

Peter
  • 553
  • 3
  • 7
  • 15

3 Answers3

2

You can get this done with a little iloc slicing magic.

df['Max'] = df.iloc[:, 1:].max(axis=1)
df['Min'] = df.iloc[:, 1:].min(axis=1)
df['Max - Min'] = df['Max'] - df['Min']

df    
    County  Year1  Year2  Year3  Year4    Max    Min  Max - Min
1  Autauga  54660  55253  51253  56253  56253  51253       5000
cs95
  • 379,657
  • 97
  • 704
  • 746
2

You can also specify that you only want to do it on numerical elements.

df['Max'] = df.max(axis=1, numeric_only=True)
df['Min'] = df.min(axis=1, numeric_only=True)
df['Max - Min'] = df['Max'] - df['Min']

# if you only need "Max - Min"
df['Max - Min'] = df.max(1, numeric_only=True) - df.min(1, numeric_only=True)

By default, max function will try to use everything. With the extra argument, it will only includes columns that contains floats, int, and boolean values in the calculation. Read more about it at DataFrame.max

Tai
  • 7,684
  • 3
  • 29
  • 49
2

IIUC:

In [43]: df = df.assign(**{'Max-Min':df.max(1)-df.min(1)})

In [44]: df
Out[44]:
    County  Year1  Year2  Year3  Year4  Max-Min
1  Autauga  54660  55253  51253  56253     5000
MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419