合并两个 pandas series:1) 列表,一个包含历史时间序列,2) 另一个包含预测时间序列

Combining two lists of pandas series:1) one containing historical time series, and 2) another containing forecast time series

这两个列表包含相同的数据系列,df_hist_list 代表历史系列,forecast_list 包含预测。我想将两个系列组合在一起,结果是两个组件的列表(即 df_Cash_DF_history 和 Cash_Due_From_Banks 将整个系列的组合结果作为一个系列)。

df_hist_list=[df_Cash_DF_history,df_Int_Dep_w_Banks_history,df_Corp_Sec_history,\
              df_USGovt_Agency_history,df_Muni_history,df_Unreal_G_L_history,\
              ...
              df_Furn_Equip_Exp_history,df_Data_Proc_Exp_history,df_Promo_Exp_history,\
              df_Oth_Op_Exp_history,df_ORE_Exp_history,df_Inc_Tax_Exp_history]


forecast_list=[Cash_Due_From_Banks,Int_Bear_Dep_w_Banks,Corp_Sec,USGovt_Agency,\
               Muni,Unreal_Gain_Loss,RE_Loans,Pers_Loans,Ag_Loans,Bus_Loans,\
               ...
               Emp_Ben_Exp,Occ_Exp,Furn_Equip_Exp,Data_Proc_Exp,Promo_Exp,\
               Oth_Op_Exp,ORE_Exp,Inc_Tax_Exp]

df_Cash_DF_history
Out[114]: 
Q1_2018    8739244.00
Q2_2018    5698279.00
Q3_2018    8849542.00
Q4_2018    1503914.00
Q1_2019    7417558.00
Q2_2019    6000285.00
Q3_2019    8697910.00
Name: TOTAL CASH & DUE FROM BANKS, dtype: object

Cash_Due_From_Banks
Out[115]: 
Q3_2019   28,697,910
Q4_2019   27,810,123
Q1_2020   26,937,969
Q2_2020   26,081,183
Q3_2020   25,239,505
Q4_2020   24,412,679
Q1_2021   23,600,453
Q2_2021   22,802,580
Q3_2021   22,018,816
dtype: float64

我假设每个 DataFrame 都有一列,您将其呈现为 Quarter 列实际上是 index.

让我们从 2 个数据帧的情况开始。

从这两个DataFrame中取出最左边的列,并连接起来 将它们垂直分成一个Series,定义如下函数:

def myConcat(df1, df2):
    s = pd.concat([df1.iloc[:,0], df2.iloc[:,0]])
    return s[~s.index.duplicated()]

我注意到你的 2 个 DataFrame 有一行来自同一季度,所以有 需要消除这种重复,为此我使用了 index.duplicated().

然后,例如对于您提供的两个源数据帧,将其称为:

myConcat(df_Cash_DF_history, Cash_Due_From_Banks)

并处理 2 lists DataFrames 并生成 list of Series 对象,由成对连接产生,运行:

[ myConcat(df1, df2) for df1, df2 in zip(df_hist_list, forecast_list) ]