NPmax()未在Python中的“if”内显示结果

2024-05-19 10:23:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试从导入的CSV文件中获取高于某个阈值的第一个值。当我使用np.where()时,我只得到th值第一次超过阈值的索引号,而不是值本身。我试图使用df.iloc[]打印它,但即使直接给出索引号,也不起作用。我做了df.iloc[Index_Abs_Ax]df.iloc[1000],其中Index_Abs_Ax是存储索引号1000的变量。但是有一个错误:没有为对象类型数据帧命名为60081的轴

第二件事是CSV文件中的一些最大值低于阈值。因此,在这种情况下,我想将阈值减半。为此,我使用类似于:if threshold_abs > np.max(resA_x1)的if语句,然后将threshold除以2。但是我可以说:无效语法

所以我想知道以下三件事:

  1. 我如何获得高于阈值的值本身,而不仅仅是索引号
  2. 为什么if语句不起作用
  3. 有没有更好的方法来编写if语句来做同样的事情?也许用一个“for”

代码如下:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from pathlib import Path 

global df
df = pd.DataFrame([])

def function_plot(x,y,z,v,Plot_ShareY=True):    #x,y,z,v are filenames for the same drop height 
    data_1 = Path(x)                            #stores the relative path location
    data_2 = Path(y)
    data_3 = Path(z)
    data_4 = Path(v)
    print(data_1)                               #prints the file location for each input argument
    print(data_2)
    print(data_3)
    print(data_4)
    
    x1 = pd.read_csv(data_1, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])  #loads the csv file from the path created above
    y1 = pd.read_csv(data_2, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])
    z1 = pd.read_csv(data_3, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])
    v1 = pd.read_csv(data_4, delimiter=";", skiprows=(1), decimal = ",", na_values = ['no info', '.'])
    
    time1 = x1.iloc[:,0]                       #stores time values of each file
    time2 = y1.iloc[:,0]
    time3 = z1.iloc[:,0]
    time4 = v1.iloc[:,0]
    
    resA_x1 = x1.iloc[:,1]                      #stores sensor output values for each sensor from each file
    resB_x1 = x1.iloc[:,2]
    resC_x1 = x1.iloc[:,3]
    resD_x1 = x1.iloc[:,4]
    
    resA_y1 = y1.iloc[:,1]
    resB_y1 = y1.iloc[:,2]
    resC_y1 = y1.iloc[:,3]
    resD_y1 = y1.iloc[:,4]
    
    resA_z1 = z1.iloc[:,1]
    resB_z1 = z1.iloc[:,2]
    resC_z1 = z1.iloc[:,3]
    resD_z1 = z1.iloc[:,4]
    
    resA_v1 = v1.iloc[:,1]
    resB_v1 = v1.iloc[:,2]
    resC_v1 = v1.iloc[:,3]
    resD_v1 = v1.iloc[:,4]
    
    threshold_abs = 0.03
    if threshold_abs > np.max(resA_x1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resB_x1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resC_x1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resD_x1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resA_y1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resB_y1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resC_y1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resD_y1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resA_z1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resB_z1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resC_z1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resD_z1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resA_v1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resB_v1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resC_v1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    elif threshold_abs > np.max(resD_v1)        
        threshold_abs = threshold_abs/2         
        print(threshold_abs)
    
        
    Index_Abs_Ax = np.where(resA_x1 > threshold_abs) [0][0]           #prints the first index where the condition holds 
    Index_Abs_Bx = np.where(resB_x1 > threshold_abs) [0][0]
    Index_Abs_Cx = np.where(resC_x1 > threshold_abs) [0][0]
    Index_Abs_Dx = np.where(resD_x1 > threshold_abs) [0][0] 
    
    print(Index_Abs_Ax)
    location = df.iloc(Index_Abs_Ax)
              
    Index_Abs_Ay = np.where(resA_y1 > threshold_abs) [0][0]           #prints the first index where the condition holds 
    Index_Abs_By = np.where(resB_y1 > threshold_abs) [0][0]
    Index_Abs_Cy = np.where(resC_y1 > threshold_abs) [0][0]
    Index_Abs_Dy = np.where(resD_y1 > threshold_abs) [0][0]
            
    Index_Abs_Az = np.where(resA_z1 > threshold_abs) [0][0]           #prints the first index where the condition holds 
    Index_Abs_Bz = np.where(resB_z1 > threshold_abs) [0][0]
    Index_Abs_Cz = np.where(resC_z1 > threshold_abs) [0][0]
    Index_Abs_Dz = np.where(resD_z1 > threshold_abs) [0][0]
            
    Index_Abs_Av = np.where(resA_v1 > threshold_abs) [0][0]           #prints the first index where the condition holds 
    Index_Abs_Bv = np.where(resB_v1 > threshold_abs) [0][0]
    Index_Abs_Cv = np.where(resC_v1 > threshold_abs) [0][0]
    Index_Abs_Dv = np.where(resD_v1 > threshold_abs) [0][0]  
    
    location_x1 = print(Index_Abs_Ax, Index_Abs_Bx, Index_Abs_Cx, Index_Abs_Dx)
    location_y1 = print(Index_Abs_Ay, Index_Abs_By, Index_Abs_Cy, Index_Abs_Dy)
    location_z1 = print(Index_Abs_Az, Index_Abs_Bz, Index_Abs_Cz, Index_Abs_Dz)
    location_v1 = print(Index_Abs_Av, Index_Abs_Bv, Index_Abs_Cv, Index_Abs_Dv)
        
    fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 1, figsize=(12,8))              #plots figure with 4 plots
    fig.suptitle('Drops at 1 cm')   
    
    ax1.plot(time1, resA_x1, 'r', resB_x1, 'g', resC_x1, 'b', resD_x1, 'y')
    
    ax1.set_ylabel('A [V]')
       
    ax2.plot(time2, resA_y1, 'r', resB_y1, 'g', resC_y1, 'b', resD_y1, 'y')
    
    ax2.set_ylabel('B [V]') 
    
    ax3.plot(time3, resA_z1, 'r', resB_z1, 'g', resC_z1, 'b', resD_z1, 'y')
    
    ax3.set_ylabel('C [V]')
    
    ax4.plot(time4, resA_v1, 'r', resB_v1, 'g', resC_v1, 'b', resD_v1, 'y')
    
    ax4.set_xlabel('Time [ms]')
    ax4.set_ylabel('D [V]') 
   
    plt.show()   
    
#function for plotting called   
function_plot('above-1-cm-A1-3.csv','above-1-cm-B2-1.csv', 'above-1-cm-C3-1.csv', 'above-1-cm-D4-1.csv')

Tags: indexthresholdnpabswheremaxv1print
1条回答
网友
1楼 · 发布于 2024-05-19 10:23:30

Numpy提供了许多advanced ways of indexing。 如果您有一个索引数组,例如示例中的fromnumpy.when,则可以使用该数组本身作为索引来获取相应的值:

>>> import numpy as np
>>> data = np.random.randint(1, 1000, size=32)
>>> data
array([356,  32, 543, 787, 690, 837, 728,  66, 782, 836, 273, 696, 831,
       589, 641, 705,  42, 812, 664,  78, 323, 286, 106, 256,  97, 152,
        53, 486, 578, 310, 459, 632])
>>> threshold = 800
>>> above = np.where(data > threshold)
>>> above
(array([ 5,  9, 12, 17]),)
>>> data[above]
array([837, 836, 831, 812])

至于if语句不起作用的原因,看起来您只是缺少condition子句后面的冒号:

>>> if 2 > 1:
...     print('2 > 1')
... 
2 > 1

更新:上述操作通常也适用于多维numpy数组:

>>> data = np.random.random(size=(10, 3))
>>> data
array([[0.569567  , 0.62124998, 0.88555157],
       [0.66075234, 0.78935382, 0.18876415],
       [0.69428287, 0.0953168 , 0.38439925],
       [0.95652594, 0.15582194, 0.53464335],
       [0.45203105, 0.63865219, 0.55559562],
       [0.49136462, 0.15233917, 0.5780308 ],
       [0.22046296, 0.77791941, 0.1071748 ],
       [0.60487268, 0.09086179, 0.37343351],
       [0.30512539, 0.18934912, 0.5882598 ],
       [0.95298127, 0.25708113, 0.53816788]])
>>> above = np.nonzero(data > 0.9)
>>> above
(array([3, 9]), array([0, 0]))
>>> data[above]
array([0.95652594, 0.95298127])

此外,如果您只对值感兴趣,还可以通过在分析之前flatten整理数据来简化问题

你的最后一个子问题相当广泛,因为有很多方法可以概括和干燥一系列条件语句。也许这可以作为一个单独的问题来解决

相关问题 更多 >

    热门问题