简体   繁体   English

如何为 X 的每个值获得 Y 的最小值?

[英]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() :您可以将groupbymin()

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.

相关问题 如何在此裁剪图像中获取 x,y,w,h 的每个值? - How to get each value of x,y,w,h in this crop image? 我应该如何找到对应于抛物线的最大或最小 y_value 的 x_value? (在 Python 中) - How should I find the x_value corresponding to the max or min y_value of a parabola? (in Python) 我如何获得 seaborn distplot 中分布峰值 y 值的 X 轴值? - How Can i Get the X axis Value for the Distributions peak y value in a seaborn distplot? 我如何绘制函数我无法在 x 或 y 中指定 x 或 y 值 - how I can draw function I can't specify x or y value in x or y 如何在 python 轮廓 map 中获得特定 (x,y) 的值 - How can get a value of specific (x,y) in python contour map 如何创建优化,其中对于 x 的值,我得到 y 使得 y 为最小值,x 为最大值 - How to create a optimization where for value of x I get y such that y is minimum and x is maximum 当 x 大于 y 时,我需要得到 x 的值 - i need to get the value of x when x greater than y Matplotlib-如何在x值是每个y值的频率的位置绘制图? - Matplotlib-How do I plot a graph where the x values are the frequency of each y value? 如何使用范围(x,y)中的每一列值创建一个 NxM 矩阵? - How to create an NxM matrix with each column value in range(x,y)? 如何在pyplot中为每个x值在y轴上绘制平均值 - How to plot average in y axis for each x value in pyplot
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM