Find the maximum and minimum values ​​of three columns in python

I would like to know how to find the difference between the maximum and minimum values ​​of three columns in python. (Column name: POPESTIMATE2010-POPESTIMATE2012) Then I have to find the maximum result among all my records. In other words, in which district did the largest absolute change in population occur between 2010-2012?

eg. If during the three-year period the population of the district is 100, 80, 130, then the biggest change in this period will be 130-180 | = 50.

enter image description here Here is my code:

import pandas as pd
census_df = pd.read_csv('census.csv')

def answer_one():
    return ((census_df['POPESTIMATE2010'],census_df ['POPESTIMATE2011'],census_df ['POPESTIMATE2012']).max()-(census_df['POPESTIMATE2010'],census_df ['POPESTIMATE2011'],census_df ['POPESTIMATE2012']).min()).max()

answer_one()
+4
source share
5 answers

, , max min , :

>>> df = pd.DataFrame({'a':[3,4,6], 'b':[22,15,6], 'c':[7,18,9]})
>>> df
   a   b   c
0  3  22   7
1  4  15  18
2  6   6   9
>>> diff = df.max() - df.min()
>>> diff
a     3
b    16
c    11
dtype: int64
>>> diff.nlargest(1)
b    16
dtype: int64

,

>>> diff.max()
16

max min , axis:

>>> diff = df.max(axis=1) - df.min(axis=1)
>>> diff
0    19
1    14
2     3
>>> diff.max()
19
+5
import pandas as pd
d = {'a':[1,2,3], 'b':[4,5,6], 'c':[7,8,9]}
df = pd.DataFrame(d)

def answer_one():
    max_1 = max(df.max())
    min_1 = min(df.min())
    return max_1 - min_1

print answer_one()

:

max_1 = max(df[['a','b']].max())
+3

max () .

min (list) min .

, , !

+1

. max min, , , diff idxmax.

import pandas as pd
census_df = pd.read_csv('census.csv')
ans_df = census_df[census_df["SUMLEV"] == 50]    
ans_df = ans_df[["STNAME", "CTYNAME", "POPESTIMATE2010", "POPESTIMATE2011", "POPESTIMATE2012"]]
ans_df = ans_df.set_index(["STNAME", "CTYNAME"])
diff = ans_df.T.max() - ans_df.T.min()
diff.idxmax()[1]
+1

I had the same problem as I solved:

f1 = census_df[census_df['SUMLEV'] == 50].set_index(['STNAME','CTYNAME'])
f1 = f1.ix[:,'POPESTIMATE2010','POPESTIMATE2011','POPESTIMATE2012','POPESTIMATE2013'
,'POPESTIMATE2014','POPESTIMATE2015']].stack()
f2 = f1.max(level=['STNAME','CTYNAME']) - f1.min(level=['STNAME','CTYNAME'])
return f2.idxmax()[1]
0
source

Source: https://habr.com/ru/post/1662771/


All Articles