result = pd.concat([result [['yearmonth','Ret','Rfree','ExRet','PE','EPS','Turnover','ROE','Swing','Mobility','D/P','VaR_GN','VaR_HS','VaR_EVT' ]], result[['PE','EPS','Turnover','ROE','Swing','Mobility','D/P','VaR_GN','VaR_HS','VaR_EVT']].shift(periods=1)], axis=1) result .columns = ['yearmonth','Ret','Rfree','ExRet','PE','EPS','Turnover','ROE','Swing','Mobility','D/P','VaR_GN','VaR_HS','VaR_EVT' 'PEL1','EPSL1','TurnoverL1','ROEL1','SwingL1','MobilityL1','D/PL1','VaR_GNL1','VaR_HSL1','VaR_EVTL1'] result = result[result['yearmonth'] >= 20080101] result.reset_index(drop=True, inplace=True) print(result)怎么修改
时间: 2023-08-20 11:33:44 浏览: 197
详解pandas数据合并与重塑(pd.concat篇)
5星 · 资源好评率100%
在这段代码中,可能需要注意以下几点:
1. 第一行代码中,result后面的空格导致了代码的格式问题,需要删除空格;
2. 第一行代码中, `[['yearmonth','Ret','Rfree','ExRet','PE','EPS','Turnover','ROE','Swing','Mobility','D/P','VaR_GN','VaR_HS','VaR_EVT' ]]` 和 `[['PE','EPS','Turnover','ROE','Swing','Mobility','D/P','VaR_GN','VaR_HS','VaR_EVT']].shift(periods=1)` 表示需要合并的两部分,它们之间需要用逗号隔开;
3. 第二行代码中,由于缺少一个逗号,需要在 `'VaR_EVT'` 和 `'PEL1'` 之间添加一个逗号;
4. 最后一行代码中,需要将`20080101`改为`20080101.0`,以保证数据类型匹配。
所以,修改后的代码如下所示:
```
result = pd.concat([result[['yearmonth','Ret','Rfree','ExRet','PE','EPS','Turnover','ROE','Swing','Mobility','D/P','VaR_GN','VaR_HS','VaR_EVT']],
result[['PE','EPS','Turnover','ROE','Swing','Mobility','D/P','VaR_GN','VaR_HS','VaR_EVT']].shift(periods=1)], axis=1)
result.columns = ['yearmonth','Ret','Rfree','ExRet','PE','EPS','Turnover','ROE','Swing','Mobility','D/P','VaR_GN','VaR_HS','VaR_EVT',
'PEL1','EPSL1','TurnoverL1','ROEL1','SwingL1','MobilityL1','D/PL1','VaR_GNL1','VaR_HSL1','VaR_EVTL1']
result = result[result['yearmonth'] >= 20080101.0]
result.reset_index(drop=True, inplace=True)
print(result)
```
希望这能够帮到你!
阅读全文