简体   繁体   English

在python中查找三列的最大值和最小值

[英]Find maximum and minimum values of three columns in a python

I would like to know how can I find the difference between maximum and minimum values of three columns in python. 我想知道如何在python中找到三列的最大值和最小值之间的差异。 (The columns name are POPESTIMATE2010-POPESTIMATE2012) Then I should find the maximum result among all my records. (列名是POPESTIMATE2010-POPESTIMATE2012)然后我应该在所有记录中找到最大结果。 in other words, Which county has had the largest absolute change in population within the period 2010-2012? 换句话说,2010 - 2012年期间哪个县的人口绝对变化最大?

eg If County Population in the 3 year period is 100, 80, 130, then its largest change in the period would be |130-80| 例如,如果3年期间的县人口为100,80,130,那么其最大的变化将是| 130-80 | = 50. = 50。

在此输入图像描述 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()

I'm not sure what should be the end result, but if you want to get the column with biggest difference between max and min value in it, then you can do it like this: 我不确定最终结果应该是什么,但是如果你想获得maxmin之间最大差异的列,那么你可以这样做:

>>> 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

and if you need just a number then 如果你只需要一个号码那么

>>> diff.max()
16

And if you want to get difference between max and min value in each row, then just do it on different axis : 如果你想在每一行中获得最大值和最小值之间的差异,那么就在不同的axis

>>> diff = df.max(axis=1) - df.min(axis=1)
>>> diff
0    19
1    14
2     3
>>> diff.max()
19
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()

and if you want to use a select group of columns: 如果要使用选定的列组:

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

max(list) gives you the max element in the list. max(list)为您提供列表中的max元素。

min(list) gives you the min element in the list. min(list)为您提供列表中的min元素。

The rest I assume should be fairly straightforward to understand! 我认为其余的应该是相当直接的理解!

You need to clean your data first and keep only the columns you need. 您需要先清理数据并仅保留所需的列。 Then transpose your data frame, and get the difference between max and min from them, and finally from the diff series get idxmax . 然后转置你的数据框,从它们得到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]

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM