我想计算不同panda(字典中总共有9个panda(列(T1、T2、T3(中最大值周围的平均值(+/-3行(,如下所示:
Depth T1 T2 T3 Ch
0 0 0.0045 0.0025 0.0045 1
1 5 0.0000 -0.0030 -0.0010 1
2 10 -0.0035 -0.0045 -0.0005 1
3 15 -0.0020 -0.0030 -0.0030 1
4 20 0.0005 -0.0005 0.0015 1
5 25 -0.0015 -0.0015 0.0005 1
我知道有些列的最大值可能在列顶部的开头,我会得到索引越界错误,所以我决定跳过这些列。以下是我尝试计算字典中熊猫的特定平均范围的代码:
for df in channels.values():
ROI_avgs = []
for column in df[['T1', 'T2','T3']]:
min = (df[[column]].idxmax() - 3)
max = (df[[column]].idxmax() + 3)
#append mean of +/- 3 rows of max in column to roi list only if min is in range
if (min - 3 <= df[[column]].idxmax() - 3 ) & (max + 3 >= df[[column]].idxmax() + 3):
ROI_avgs.append(df[[column]].iloc[[min,max]].mean(axis=0))
else:
ROI_avgs.append(math.nan)
df.loc[len(ROI_avgs)] = ROI_avgs
当我运行这个时,我得到错误,";级数的真值是模糊的。使用a.empty、a.bool((、a.item((、.any((或.all((;有什么建议吗?
我认为这是在谈论这一行:
if (min - 3 <= df[[column]].idxmax() - 3 ) & (max + 3 >= df[[column]].idxmax() + 3):
将一个值与一个系列进行比较会产生一系列的dtype bool,也许你想要的是:
if min >= 0 and max < len(df):