我正在建立一个收入预测模型,其中,我需要的一列取决于当月,并且是一个变量。
我已将变量标记为curr_month作为用户输入。依此,我还导出了剩余月份的另一个变量rem_month。
现在,我需要创建的列是除当前月份以外的其余列的总和。此外,输入列的更改如下:
Jan-May: Planned
June-October: Mid
November: Final
December: Planned for next year.
我为每个月创建了一个清单,如下所示:
Revenue_Jan = ['P_2019_Feb','P_2019_Mar','P_2019_Apr','P_2019_May','P_2019_Jun','P_2019_Jul','P_2019_Aug','P_2019_Sep','P_2019_Oct','P_2019_Nov','P_2019_Dec']
Revenue_Feb = ['P_2019_Mar','P_2019_Apr','P_2019_May','P_2019_Jun','P_2019_Jul','P_2019_Aug','P_2019_Sep','P_2019_Oct','P_2019_Nov','P_2019_Dec']
Revenue_Mar = ['P_2019_Apr','P_2019_May','P_2019_Jun','P_2019_Jul','P_2019_Aug','P_2019_Sep','P_2019_Oct','P_2019_Nov','P_2019_Dec']
Revenue_Apr = ['P_2019_May','P_2019_Jun','P_2019_Jul','P_2019_Aug','P_2019_Sep','P_2019_Oct','P_2019_Nov','P_2019_Dec']
Revenue_May = ['P_2019_Jun','P_2019_Jul','P_2019_Aug','P_2019_Sep','P_2019_Oct','P_2019_Nov','P_2019_Dec']
Revenue_Jun = ['M_2019_Jul','M_2019_Aug','M_2019_Sep','M_2019_Oct','M_2019_Nov','M_2019_Dec']
Revenue_Jul = ['M_2019_Aug','M_2019_Sep','M_2019_Oct','M_2019_Nov','M_2019_Dec']
Revenue_Aug = ['M_2019_Sep','M_2019_Oct','M_2019_Nov','M_2019_Dec']
Revenue_Sep = ['M_2019_Oct','M_2019_Nov','M_2019_Dec']
Revenue_Oct = ['M_2019_Nov','M_2019_Dec']
Revenue_Nov = ['F_2019_Nov','F_2019_Dec']
Revenue_Dec = ['P_2020_Jan','P_2020_Feb','P_2020_Mar','P_2020_Apr','P_2020_May','P_2020_Jun','P_2020_Jul','P_2020_Aug','P_2020_Sep','P_2020_Oct','P_2020_Nov','P_2020_Dec']
现在,我打算创建一个最后的列“ Landing”,该列将是根据currant month在特定列表中提到的所有列的总和。
#var="Revenue_"+ curr_month
#print (var)
--> Revenue_Sep
现在我打算使用df.loc:
#df['Landing']= df.loc[:,Revenue_Sep].sum(axis=1)
这给了我正确的输出。
但是,如果我决定使用变量,它将无法声明“ dataframe中未定义的Revenue_Sep列”
#df['Landing']= df.loc[:,var].sum(axis=1) #Need help with this.
由于模型的其余部分已经完全准备好,所以我只需要最后一条语句的帮助,这样就不必再为整个模型进行更改了。