I want to find the first quartile in which the mentioned condition is satisfied but i am facing this error
1 2 3 4 5 6 7 8 | x=[ ] for i,row in enumerate (df3['GDP']): if((df3.iloc[i]<df3.iloc[i-1]) & (df3.iloc[i+1]<df3.iloc[i])): x.append(i) print(x) |
它在显示这个错误序列的真值不明确。使用a.empty、a.bool()、a.item()、a.any()或a.all()。
IIUC:您需要的是
考虑数据帧
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | np.random.seed([3,1415]) df3 = pd.DataFrame(dict(GDP=np.random.randint(10, size=15))) print(df3) GDP 0 0 1 2 2 7 3 3 4 8 5 7 6 0 7 6 8 8 9 6 10 0 11 2 12 0 13 4 14 9 |
解决方案使用
1 2 3 4 5 | g = df3.GDP x = np.arange(len(g))[g.gt(g.shift()) & g.lt(g.shift(-1))] print(x) [ 1 7 13] |
你实际问题的答案是你不能评估一个
1 | if((df3.iloc[i]<df3.iloc[i-1]) & (df3.iloc[i+1]<df3.iloc[i])): |