Python 两个数据帧之间的外推(舍入问题)
我在尝试在两个数据帧之间进行推断时遇到问题Python 两个数据帧之间的外推(舍入问题),python,pandas,Python,Pandas,我在尝试在两个数据帧之间进行推断时遇到问题 df1 = pd.DataFrame([(50,100),(150,250),(250,300)], columns=['a','b']) df2 = pd.DataFrame([(100,200),(300,400),(500,600)], columns=['a','b']) 我试过了 cagr_7 = (df2/df1)**(1/5) - 1 f = lambda c: c + c*cagr_7 但通常情况下,由于每年都有报告,所以中间
df1 = pd.DataFrame([(50,100),(150,250),(250,300)], columns=['a','b'])
df2 = pd.DataFrame([(100,200),(300,400),(500,600)], columns=['a','b'])
我试过了
cagr_7 = (df2/df1)**(1/5) - 1
f = lambda c: c + c*cagr_7
但通常情况下,由于每年都有报告,所以中间年份没有意义。是否还有另一种外推选择,可能是利用差异并跨年应用
我试图在5个不同的年份之间进行推断。例如2016年至2021年。每个数据帧是一年
我需要帮助 IIUC,这可能是一个开始:
df1 = pd.DataFrame([(50,100),(150,250),(250,300)], columns=['a','b'])
df2 = pd.DataFrame([(100,200),(300,400),(500,600)], columns=['a','b'])
pd.concat([df1, df2], keys=[2016,2021])\ #concat the datafrmaes using keys to define year
.unstack()\ #reshape dataframe
.reindex(np.arange(2016,2022,1))\ #reindex adding years to interpolate to
.interpolate(method='index')\ #interpolate using method index
.stack(0)\ #reshape dataframe to desired format
.T
输出:
2016 2017 2018 2019 2020 2021
a b a b a b a b a b a b
0 50.0 100.0 60.0 120.0 70.0 140.0 80.0 160.0 90.0 180.0 100.0 200.0
1 150.0 250.0 180.0 280.0 210.0 310.0 240.0 340.0 270.0 370.0 300.0 400.0
2 250.0 300.0 300.0 360.0 350.0 420.0 400.0 480.0 450.0 540.0 500.0 600.0
在这些数据框中,你有多少年?每个数据框都是一年