预测历史数字

时间:2018-12-24 22:10:27

标签: python pandas forecasting

我想根据历史数据预测分配。

用户提供的手动输入:

year    month     x          y          z          k
2018    JAN  9,267,581   627,129     254,110     14,980 
2018    FEB  7,771,691   738,041     217,027     17,363 

历史人物的输出:

year  month segment pg  is_p    x   y   z   k
2018    JAN A   p   Y   600 600 600 600
2018    JAN A   p   N   200 200 200 200
2018    JAN B   r   Y   400 400 400 400
2018    JAN A   r   Y   400 400 400 400
2018    JAN A   r   N   400 400 400 400
2018    JAN B   r   N   300 300 300 300
2018    JAN C   s   Y   200 200 200 200
2018    JAN C   s   N   10  10  10  10
2018    JAN C   t   Y   11  11  11  11
2018    JAN C   t   N   12  12  12  12
2018    FEB A   p   Y   789 789 789 789
2018    FEB A   p   N   2093874 2093874 2093874 2093874

我尝试从总数中计算is_p的分配,就像我说要添加某些列以计算分配的百分比:

  1. %ofx_segment = 600 + 200 + 400 + 400/600 + 200 + 400 + 400 + 400 + 300 + 200 + 10 + 11 + 12。这将给我细分带来多少x y,z,k同样如此
  2. 我将人工输入的9276581 * %ofx_segment乘以计算segment_x的值
  3. 然后,我计算%_pg。对于2018年1月的A区,%_pg = 600 + 200/600 + 200 + 400 + 400
  4. 然后,我将步骤2中收到的手动输入乘以*从3中收到的%pg在A段的pg中表示'p'
  5. 然后,最后我将计算is_p的百分比,我将计算%Y或%N 段%Y中A的pg中pg的p = 600/600 + 200。
  6. 从步骤5接收到的值必须与从4接收到的输出相乘。

import pandas as pd
first=pd.read_csv('/Users/arork/Downloads/first.csv')
second=pd.read_csv('/Users/arork/Downloads/second.csv')
interested_columns=['x','y','z','k']
second=pd.read_csv('/Users/arork/Downloads/second.csv')
interested_columns=['x','y','z','k']
primeallocation=first.groupby(['year','month','pg','segment'])[['is_p']+interested_columns].apply(f)
segmentallocation=first.groupby(['year','month'])[['segment']+interested_columns].apply(g)
pgallocation=first.groupby(['year','month','segment'])[['pg']+interested_columns].apply(h)
segmentallocation['%of allocation_segment x']
np.array(second)
func = lambda x: x * np.asarray(second['x'])
segmentallocation['%of allocation_segment x'].apply(func)

1 个答案:

答案 0 :(得分:0)

您需要连接这两个数据帧以执行两列的乘法。

merged_df = segmentallocation.merge(second,on=['year','month'],how='left',suffixes=['','_second'])

for c in interested_columns:
        merged_df['allocation'+str(c)] = merged_df['%of allocation'+str(c)] * merged_df[c] 

merged_df


    year    month   segment x   y   z   k   %of allocationx %of allocationy %of allocationz %of allocationk x_second    y_second    z_second    k_second    allocationx allocationy allocationz allocationk
0   2018    FEB A   2094663 2094663 2094663 2094663 1.000000    1.000000    1.000000    1.000000    7,771,691   738,041 217,027 17,363  2.094663e+06    2.094663e+06    2.094663e+06    2.094663e+06
1   2018    JAN A   1600    1600    1600    1600    0.631662    0.631662    0.631662    0.631662    9,267,581   627,129 254,110 14,980  1.010659e+03    1.010659e+03    1.010659e+03    1.010659e+03
2   2018    JAN B   700 700 700 700 0.276352    0.276352    0.276352    0.276352    9,267,581   627,129 254,110 14,980  1.934465e+02    1.934465e+02    1.934465e+02    1.934465e+02
3   2018    JAN C   233 233 233 233 0.091986    0.091986    0.091986    0.091986    9,267,581   627,129 254,110 14,980  2.143269e+01    2.143269e+01    2.143269e+01    2.143269e+01