pkhiev :
if I have a sample dataframe like this:
>>> import pandas as pd
>>> a = [100,300,200,100,700,600,400,600]
>>> i = ["2000", "2001", "2002", "2003", "2004", "2005", "2006", "2007"]
>>> df = pd.DataFrame(a, index = i, columns = {"gdp"})
>>> df
gdp
2000 100
2001 300
2002 200
2003 100
2004 700
2005 600
2006 400
2007 600
res=[]
I want to iterate through the rows, and the conditions are: if row(x+1) - row(x) <0 & row (x+2)-row(x+1)<0 res.append(index[x])
So in this example, I would get a list of [2001, 2004]
I'm not sure how to write the code for this. Thank you!
jezrael :
I prefer non loop solution, because better performance - use Series.shift
, subtract by Series.sub
and compare with Series.lt
for less, last filter by boolean indexing
with DataFrame.loc
, if need also filter by column name gdp
:
s1 = df['gdp'].shift(-1)
s2 = df['gdp'].shift(-2)
m = s1.sub(df['gdp']).lt(0) & s2.sub(s1).lt(0)
out = df.loc[m, 'gdp']
print (out)
2001 300
2004 700
Name: gdp, dtype: int64
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=291392&siteId=1