[英]How can I get min value of Y for each value of X?
I have two lists X and Y.我有两个列表 X 和 Y。
X = [0.034, 0.026, 0.028, ...]
Y = [0.0099, 0.0065, 0.0061, ...]
I want to get min value of Y for each value of X. I tried with groupby approach.我想为 X 的每个值获得 Y 的最小值。我尝试使用 groupby 方法。 I need to get points on the border.
我需要在边境获得积分。 which are marked in black in the figure below.
在下图中用黑色标出。 I have X and Y coordinate for all the points.
我有所有点的 X 和 Y 坐标。
You can use groupby
with min()
:您可以将
groupby
与min()
:
import pandas as pd
# two y values for 0.034 and 0.026
X = [0.034, 0.026, 0.028, 0.034, 0.026]
Y = [0.0099, 0.0065, 0.0061, 0.1, 0.000001]
df = pd.DataFrame({'x': X, 'y': Y})
x y
0 0.034 0.009900
1 0.026 0.006500
2 0.028 0.006100
3 0.034 0.100000
4 0.026 0.000001
df.groupby('x')['y'].min()
x
0.026 0.000001
0.028 0.006100
0.034 0.009900
Name: y, dtype: float64
As you deal with floating point numbers, you might want to do a groupby
with rounded numbers (eg 5 digits):当您处理浮点数时,您可能想要使用四舍五入的数字(例如 5 位数字)进行
groupby
:
df.groupby(df['x'].round(5))['y'].min()
That will give 8018 xy
pairs using the dataset you provided.这将使用您提供的数据集提供 8018 个
xy
对。
This is one way in Python to get min values .. See the as an example这是 Python 中获取最小值的一种方法.. 参见示例
list1, list2 = [123, 223, 154, 235], [456, 700, 200]
print "min value element : ", min(list1)
print "min value element : ", min(list2)
When we run above program, it produces following result −当我们运行上面的程序时,它会产生以下结果 -
min value element : 123
min value element : 200
Pandas solution as stated in above answer:上述答案中所述的 Pandas 解决方案:
df.groupby('x')['y'].min()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.