Max and Min value for each colum of one Dataframe

女生的网名这么多〃 提交于 2020-01-23 12:54:28

问题


Give this dataframe '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] ]

回答1:


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

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:

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:

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 describe and loc to get what you want:

In [212]:

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



回答2:


Pandas introduced the agg method for dataframes which makes this even easier:

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

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



回答3:


>>> 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.describe method helpful: http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.describe.html



来源:https://stackoverflow.com/questions/29276301/max-and-min-value-for-each-colum-of-one-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!