简体   繁体   English

如何在 pandas dataframe 的多列中最小/最大值?

[英]How to min/max value in multiple columns of a pandas dataframe?

how can i get one min/max value of several columns from a dataframe?如何从 dataframe 中获取多个列的一个最小值/最大值? I could not find a simple way to get these values, only with looping over the columns or converting the dataframe multiple times.我找不到获取这些值的简单方法,只能循环遍历列或多次转换 dataframe。 I think there must be a better way to solve this.我认为必须有更好的方法来解决这个问题。

For example, here are some code...例如,这里有一些代码......

import pandas as pd

df = pd.DataFrame([[0,1,2,3],
                  [6,5,None,pd.NaT],
                  [8,None,9,None],
                  [None,12,7,14]], columns=list('ABCD'))

... this is what's the dataframe looks like and I want the min/max of column 'C' and 'D'. ...这就是 dataframe 的样子,我想要列“C”和“D”的最小值/最大值。

     A     B    C     D
0  0.0   1.0  2.0     3
1  6.0   5.0  NaN   NaT
2  8.0   NaN  9.0  None
3  NaN  12.0  7.0    14

What is a good way to do this?这样做的好方法是什么?

Additional Note: The result of both columns ['C','D'] should be one value for min (2) and one value for max (14)附加说明:两列 ['C','D'] 的结果应为最小值 (2) 和最大值 (14) 的一个值

Use DataFrame.agg with selected columns by list - ['C','D'] :DataFrame.agg与列表中的选定列一起使用 - ['C','D']

df1 = df[['C','D']].agg(['min','max'])
print (df1)
       C   D
min  2.0   3
max  9.0  14

EDIT: For 2 scalars you can use:编辑:对于 2 个标量,您可以使用:

s = df[['C','D']].stack()
print (s)
0  C     2
   D     3
2  C     9
3  C     7
   D    14
dtype: object

a = s.max()
print (a)
14

b = s.min()
print (b)
2

You can use,您可以使用,

df[['C','D']].min().min()
2.0

and

df[['C', 'D']].max().max()
14.0

If you are not sure about the column names, and want to do it on last 2 columns, you can do this:如果您不确定列名,并想在最后 2 列上这样做,您可以这样做:

In [2138]: df.iloc[:, -2:].agg(['max', 'min'])
Out[2138]: 
       C   D
max  9.0  14
min  2.0   3

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

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