本文介绍了计算季度的衰退和复苏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给定这个数据框
GDP quarter
0 250 2015q1
1 260 2015q1
2 250 2015q3
3 240 2015q4
4 250 2016q1
5 260 2016q2
我如何确定哪些季度是衰退,哪些季度是复苏?
How can I figure out what quarters are recessions and what quarters are recoveries?
经济衰退是指 GDP 连续两个季度下降
A Recession is when GDP is decreasing two quarters in a row
复苏是指 GDP 下降,但现在正在上升.
A Recovery is when GDP was decreasing, but is now on the rise.
推荐答案
这里需要用到Series.shift函数
Here you need to use the Series.shift function
由于您的数据不容易复制,我将创建自己的示例数据.
Since your data is not easily copypastable, I'll just create my own example data.
df = pd.DataFrame({"quarter":["2015q1", "2015q1", "2015q3", "2015q4", "2016q1", "2016q2"], "GDP": [250, 260, 250, 240, 250, 260]}
df['Last_GDP'] = df.GDP.shift(1)
df['Increase'] = df.GDP > df.Last_GDP
df['Last_Increase'] = df.Increase.shift(1)
df.ix[(~df.Last_Increase.fillna(False) & ~df.Increase), 'Re-x'] = "Recession"
df.ix[(~df.Last_Increase.fillna(False) & df.Increase), 'Re-x'] = "Recovery"
df.ix[df.Last_Increase.isnull(), 'Re-x'] = "Unknown"
GDP quarter Last_GDP Increase Last_Increase Re-x
0 250 2015q1 NaN False NaN Unknown
1 260 2015q1 250.0 True False Recovery
2 250 2015q3 260.0 False True NaN
3 240 2015q4 250.0 False False Recession
4 250 2016q1 240.0 True False Recovery
5 260 2016q2 250.0 True True NaN
这篇关于计算季度的衰退和复苏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!