pandas 一个数据帧的每一列的最大值和最小值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29276301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:06:26  来源:igfitidea点击:

Max and Min value for each colum of one Dataframe

pythonlistpandasmaxmin

提问by Yari

Give this dataframe 'x':

给这个数据框'x':

    col1 col2 col3 col4
    0     5   -2    1 
   -5     2   -1    9
    3    -7    3    5

How I could get a list of pairs with the min and max of each column? The result would be:

我如何获得每列的最小值和最大值对的列表?结果将是:

list = [ [-5 , 3], [-7 , 5], [-2 , 3], [1 , 9] ]

回答by EdChum

You could define a function and call applypassing the function name, this will create a df with min and max as the index names:

您可以定义一个函数并调用apply传递函数名称,这将创建一个以 min 和 max 作为索引名称的 df:

In [203]:

def minMax(x):
    return pd.Series(index=['min','max'],data=[x.min(),x.max()])


df.apply(minMax)
Out[203]:
     col1  col2  col3  col4
min    -5    -7    -2     1
max     3     5     3     9

If you insist on a list of lists we can transpose the df and convert the values to a list:

如果您坚持使用列表列表,我们可以转置 df 并将值转换为列表:

In [206]:

def minMax(x):
    return pd.Series(index=['min','max'],data=[x.min(),x.max()])


df.apply(minMax).T.values.tolist()
Out[206]:
[[-5, 3], [-7, 5], [-2, 3], [1, 9]]

The function itself is not entirely necessary as you can use a lambda instead:

该函数本身并不是完全必要的,因为您可以使用 lambda 代替:

In [209]:

df.apply(lambda x: pd.Series([x.min(), x.max()])).T.values.tolist()
Out[209]:
[[-5, 3], [-7, 5], [-2, 3], [1, 9]]

Note also that you can use describeand locto get what you want:

另请注意,您可以使用describeloc来获得您想要的:

In [212]:

df.describe().loc[['min','max']]
Out[212]:
     col1  col2  col3  col4
min    -5    -7    -2     1
max     3     5     3     9

回答by MightyCurious

Pandas introducedthe agg method for dataframes which makes this even easier:

Pandas为数据帧引入了 agg 方法,这使得这更容易:

df.agg([min, max])
Out[207]: 
     col1  col2  col3  col4
min    -5    -7    -2     1
max     3    49     6     9

That's all. Conversion into a list, if needed, can then be done as described in the accepted answer. As a bonus, this can be used with groupby also (which doesn't work well with apply):

就这样。如果需要,可以按照接受的答案中的描述转换为列表。作为奖励,这也可以与 groupby 一起使用(不适用于 apply):

df.groupby(by='col1').agg([min, max])
Out[208]: 
     col2     col3     col4    
      min max  min max  min max
col1                           
-5      2   2   -1  -1    9   9
 0      5  49   -2   6    1   6
 3     -7  -7    3   3    5   5

回答by grechut

>>> df = pd.DataFrame([[0, 5], [-5, 2], [3, -7]])
>>> list = [ [min, max] for min, max in zip(df.min(), df.max()) ]
>>> list
[[-5, 3], [-7, 5]]

Other note: You might find DataFrame.describemethod helpful: http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.describe.html

其他注意事项:您可能会发现DataFrame.describe方法有用:http: //pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.describe.html