获取衰退开始和衰退结束的季度以及最低GDP的季度 [英] Getting the quarter where recession start and recession ends along with the quarter of minimum gdp

查看:97
本文介绍了获取衰退开始和衰退结束的季度以及最低GDP的季度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Quarter:    GDP:   GDP change:  change
1999q3     9           --         ------
1999q4    10           1          increase
2000q1     9          -1          decline
2000q2     8          -1          decline
2000q3     7          -1          decline
2000q4     6          -1          decline
2001q1     8           1          increase
2001q2     11          3          increase
2001q3     12          1          increase

这里是已处理的数据帧现在,我需要单独列出衰退开始,衰退结束和衰退底部的所有季度

Here is the Processed dataFrame Now I need separate list of all the quarters in which recession starts ,recession ends and bottom of recession

在上面的示例中,经济衰退的开始是"2000q1",因为那时GDP开始下降

The start of recession in the example above is '2000q1'because the GDP decline started then

经济衰退结束是"2001q2"

recession end is ''2001q2'

经济衰退的底部为"2000q4",因为它在开始和结束之间的GDP最低

recession bottom is '2000q4' because it has the minimum GDP between start and end

推荐答案

recession_start = False
recession_gdp = 0
quarter = {'start': None, 'end': None, 'min': None}
min_gdp = 10000


final_list = []


for i, row in frame.iterrows():

    if row['change'] < 0 and not recession_start:
        recession_start = True
        recession_gdp = frame[i - 1]['gdp']
        quarter['start'] = row['quarter']

    if row['gdp'] < min_gdp and recession_start:
        min_gdp = row['gdp']
        quarter['min'] = row['quarter']

    if recession_start and row['gdp'] >= recession_gdp:
        quarter['end'] = row['quarter']
        final_list.append(quarter)
        # reset all values
        recession_start = False
        recession_gdp = 0
        quarter = {'start': None, 'end': None, 'min': None}

        min_quarter = None
        min_gdp = 10000

final_list有最终答案.对不起,我不认识熊猫,可能有一些错误.这更像是一个简短的算法..但是您只需进行一些重构就可以正确处理它.希望对您有帮助!..

final_list has the final answer. I'm sorry I do not know pandas and there may be some errors. This is more like a brief algorithm.. but you can get it right with just a little refactoring. Hope it helps you!..

我还假设当gdp等于或大于gpp刚好在衰退开始之前(在您的示例中为10)时,衰退结束.

Also I'm assuming the recession ends when the gdp is equal to or greater than the gdp 'just before' the recession started which in your example is 10.

这篇关于获取衰退开始和衰退结束的季度以及最低GDP的季度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆