我一直在尝试从导入的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。但是我可以说:无效语法
所以我想知道以下三件事:
代码如下:
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')
Numpy提供了许多advanced ways of indexing。 如果您有一个索引数组,例如示例中的from
numpy.when
,则可以使用该数组本身作为索引来获取相应的值:至于
if
语句不起作用的原因,看起来您只是缺少condition子句后面的冒号:更新:上述操作通常也适用于多维
numpy
数组:此外,如果您只对值感兴趣,还可以通过在分析之前flatten整理数据来简化问题
你的最后一个子问题相当广泛,因为有很多方法可以概括和干燥一系列条件语句。也许这可以作为一个单独的问题来解决
相关问题 更多 >
编程相关推荐