我想计算不同熊猫(字典中总共 9 只熊猫)列(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()、a.any() 或 a.all() .”有什么建议吗?
答案 0 :(得分:0)
我认为这是在谈论这条线:
if (min - 3 <= df[[column]].idxmax() - 3 ) & (max + 3 >= df[[column]].idxmax() + 3):
将值与系列进行比较会产生一系列 dtype bool,也许您想要的是:
if min >= 0 and max < len(df):