返回数据帧中两列的最大值(Pandas)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46801367/
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-14 04:39:21  来源:igfitidea点击:

Return the maximum value of two columns in a dataframe (Pandas)

pythonpandas

提问by Flora

I currently have a dataframe and would like to return the minimum value of two columns (eg. X and Y).

我目前有一个数据框,想返回两列(例如 X 和 Y)的最小值。

I have tried:

我试过了:

print(df.loc[:, ['X', 'Y']].min())

However, it prints out:

但是,它打印出:

Control NSAF   -9.851210
Wild NSAF      -9.730507
dtype: float64

Whereas I just want -9.851210. Is there a way to just get the single minimum number?

而我只想要-9.851210。有没有办法只获得单个最小数字?

Thank you

谢谢

回答by YOBEN_S

Add one more min

再添加一个 min

print(df.loc[:, ['X', 'Y']].min().min())

回答by Chetan_Vasudevan

The below given example may be useful for you to find

下面给出的示例可能对您有用

maximum and minimum of "X" and "Y" columns

“X”和“Y”列的最大值和最小值

  df[["X", "Y"]].max(axis=1)
  df[["X", "Y"]].min(axis=1)

回答by piRSquared

use the numpy minmethod on the underlying valuesattribute

min在底层values属性上使用 numpy方法

df.loc[:, ['X', 'Y']].values.min()

You can even find the locations of 'X'and 'Y'ahead of time

你甚至可以找到的地点'X''Y'时间提前

j = [df.columns.get_loc(c) for c in ['X', 'Y']]
df.values[:, j].min()