[英]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.