-1
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) 

It is showing this ERROR The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Here's a sample of my data

  • @piRSquared i am a noob in programming and through those 2 questions I couldn't figure out my problem. I would be highly grateful if you would remove this mark as duplicate tag and answer my question – Varun Saxena Apr 08 '17 at 08:57
  • You've got a few things wrong with your code. We can help more if you tell us what you are trying to do. The question and answers I linked answers the question of why you are getting the error. However, it seems you want to know how to accomplish some task, which is more than the question you asked. You'd be better served, as would the community, if you clarified what it is you are trying to do, what you expect as a result, and what you've tried so far. – piRSquared Apr 08 '17 at 09:00
  • i want to find out the quartile which satisfies the mentioned condition – Varun Saxena Apr 08 '17 at 09:02
  • You should click the [***edit***](http://stackoverflow.com/posts/43291830/edit) link and make you question as clear as possible. Also, if you want to use SO as a resource, read [***MCVE***](http://stackoverflow.com/help/mcve) and [***HowToAsk***](http://stackoverflow.com/help/how-to-ask) – piRSquared Apr 08 '17 at 09:05

1 Answers1

0

IIUC:
You want the ordinal positions of where the 'GDP' values were greater than the last one and less then the next one.

Consider the dataframe df3

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

solution
using shift

g = df3.GDP
x = np.arange(len(g))[g.gt(g.shift()) & g.lt(g.shift(-1))]
print(x)

[ 1  7 13]

The answer to your actual question is that you can't assess the truth of a pd.DataFrame or pd.Series and that's exactly what you were trying to do with

if((df3.iloc[i]<df3.iloc[i-1]) & (df3.iloc[i+1]<df3.iloc[i])):
piRSquared
  • 285,575
  • 57
  • 475
  • 624
  • What do you mean by I can't assess the truth of a dataframe? – Varun Saxena Apr 08 '17 at 09:20
  • take the `pd.Series` `g` referenced in my asnwer. `g > 3` evaluates to a `pd.Series` of `True`s and `False`s. It isn't `True` or `False` on it's own. `bool(g > 3)` returns the same error you asked about. You get it because `pandas` recognizes when `bool(pd.Series)` is called and raises that exception. It does this because it makes no sense to ask if the series of boolean values is itself `True` or `False`. People fall into this trap as they are looking to iterate over the series of booleans and they mistakenly evaluate the whole series. – piRSquared Apr 08 '17 at 09:26