Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

python - Forecasting basis the historical figures

I want to forecast the allocations basis the historical figures.

Manual Input provided by the user:

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 

Output from Historical figures:

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

I have tried calculating the allocation of is_p from the total like let say I add certain columns to calculate the %of allocation:

  1. %ofx_segment= 600+200+400+400/600+200+400+400+400+300+200+10+11+12. This will give me how much x is contributed from segment The same goes with y,z,k
  2. I multiply the manual input that is 9276581 * %ofx_segment to calculate the value of segment_x
  3. Then, I calculate %_pg. For segment A for Jan 2018, %_pg= 600+200/600+200+400+400
  4. Then, I multiply the manual input received from Step 2 * %pg received from 3 for 'p' in pg for A segment
  5. Then, at last, I will calculate % of is_p, I will calculate % Y or %N for p in pg for A in segment % Y is =600/600+200.
  6. The value received from Step 5 has to be multiplied to the output received from 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to join those two dataframes to perform multiplication of two columns.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...