pandas - How to determine column values changing in a particular direction? -
i have dataframe consisting of 1 column of periods (year , quarters) , column of productivity numbers period. task identify period where, example, have 2 consecutive quarters of productivity decline; or, similarly, 2 consecutive quarters of growth. imagine can use brute force , loop on rows looking @ several rows @ time, reading might have "shift" function -- dont understand how works. thank help
1971q1 1,137.8 1971q2 1,159.4 1971q3 1,180.3 1971q4 1,173.6 1972q1 1,163.8 1972q2 1,140.1 1972q3 1,145.8 1972q4 1,150.0
try buddy
#define growth rate df['growth_rate'] = np.log(df.production) - np.log(df.production).shift(1) #a recession when there have been 2 quarters of negative growth. df['recession'] = (df['growth_rate'] < 0 ) & (df['growth_rate'].shift(1) < 0 )
Comments
Post a Comment