简体   繁体   English

如何从熊猫数据框中找到最小和最大AND对应的x值?

[英]How to find the min and max AND corresponding x-value from a pandas data frame?

I want to be able to find the min and max of a pandas data frame AND the corresponding X-value. 我希望能够找到熊猫数据框的最小值和最大值以及相应的X值。 As of now, I just have it find the Y-Value max and min, but I can't figure out how to provide a corresponding X-Value. 到目前为止,我只是找到了Y值的最大值和最小值,但是我不知道如何提供相应的X值。 What can I do to get this corresponding value? 我该怎么做才能获得相应的价值?

So this finding of the min and max is included in a loop for writing an excel file and I want to eventually write out them max or min values in the excel file as well. 因此,对min和max的发现包含在编写excel文件的循环中,我最终希望在excel文件中也将它们的max或min值写出来。 I have tried doing this function to give the corresponding values 我已经尝试过执行此功能以提供相应的值

maxVal = dfspectra.loc[dfspectra['Current (mA)'].idxmax()] minVal = dfspectra.loc[dfspectra['Current (mA)'].idxmin()] maxVal = dfspectra.loc [dfspectra ['Current(mA)']。idxmax()] minVal = dfspectra.loc [dfspectra ['Current(mA)']。idxmin()]

This did not work and only gives me NaN or says that the data frame cannot be callable 这是行不通的,只能给我NaN或说数据帧无法调用

Here is the initial introduction of the data frame: 这是数据帧的初始介绍:

for filename in filestoprocess:
    baseName=os.path.basename(os.path.basename(filename))
    dfspectra=pd.read_csv(filename, skiprows = 1, delimiter =        "\s+|\t+|\0+", header = None, names = ['Potential (V)','Current (mA)'],     engine='python', skipfooter=1,skipinitialspace=True)
    fig, ax = plt.subplots()
    ax.plot(dfspectra['Potential (V)'],dfspectra['Current (mA)'],     "ok")
    ax.set(xlabel='Potential (V)', ylabel='Current (mA)', title='CV Data')

And here is my attempt of finding the max/min and corresponding x-value: 这是我尝试找到最大值/最小值和相应的x值的尝试:

 ymax = np.max(dfspectra['Current (mA)'])
 ymin = np.min(dfspectra['Current (mA)'])
 maxVal = dfspectra.loc[dfspectra['Current (mA)'].idxmax()]
 minVal = dfspectra.loc[dfspectra['Current (mA)'].idxmin()]

When I put min or maxVal into the console, it gives my full data frame and when I try maxVal(dfspectra['Current (mA)'], it says that the data frame is not callable. I thought that the numpy function finds the Y-value while the loc function gives the X-value. 当我将min或maxVal放入控制台时,它给出了完整的数据帧,而当我尝试maxVal(dfspectra ['Current(mA)']时,它表示该数据帧不可调用。我认为numpy函数找到了Y值,而loc函数给出X值。

Try this - 尝试这个 -

indx_max = np.argmax(dfspectra['Current (mA)'])
x_max = dfspectra.iloc[indx_max]

The first statement finds the index that contains the maximum value in column Current (mA) in your dfspectra . 第一条语句在dfspectra找到包含“ Current (mA)列中最大值的索引。 The second statement returns the row corresponding to the index from first row. 第二条语句返回与第一行的索引相对应的行。 For minima, use np.argmin . 对于最小值,请使用np.argmin

indx_min = np.argmin(dfspectra['Current (mA)'])
x_min = dfspectra.iloc[indx_min]

暂无
暂无

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

相关问题 如何按天对日期进行分组并在 Pandas 数据框或 python 中查找最小值和最大值 - How to group date by day and find min and max value in pandas data frame or python 使用 Pandas,如何从一组列中找到最小值/最大值和索引,满足相应的一组列的条件? - Using Pandas, how can I find the min/max value and index from one set of columns, satisfying condition on a corresponding set of columns? 我应该如何找到对应于抛物线的最大或最小 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) 为熊猫数据框的单独列(来自特定列范围)的最大值选择相应的列值 - Select corresponding column value for max value of separate column(from a specific range of column) of pandas data frame 从pandas数据框中获取多个最小和最大日期 - Getting multiple min and max dates from a pandas data frame pandas 数据框中的最大值和最小值 - max and min values in pandas data frame 找到与最大直方图对应的x值 - Find the x value corresponding to a histogram max 如何计算特定 x 值处的多项式? - How to evaluate a polynomial at a specific x-value? 如何从 python 中的数组中找到最小值和最大值? - How to find min and max value from an arrary in python? 如何从 Python 中的该数据集中找到最大值或最小值? - How do I find the Max or Min value from this dataset in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM