Ode Proceeds Pos Amount Positions Target Weighting Additions
0 676 30160 FPE 51741.25000 5 0.10 0.187636 NaN
1 676 30160 HFA 57299.63616 5 0.20 0.207794 NaN
2 676 30160 PFL 60437.40563 5 0.20 0.219173 NaN
3 676 30160 PSO 53053.57410 5 0.15 0.192396 NaN
4 676 30160 RNS 53220.36636 5 0.20 0.193001 NaN
5 953 34960 PFL 8506.19390 1 0.20 1.000000 NaN
6 637 14750 PFL 8341.21701 3 0.20 0.302517 NaN
7 637 14750 PSO 12669.65078 3 0.15 0.459499 NaN
8 637 14750 RNS 6561.85824 3 0.20 0.237984 NaN
9 673 12610 FPE 31220.47500 5 0.10 0.175041 NaN
10 673 12610 HFA 34020.29280 5 0.20 0.190738 NaN
11 673 12610 PFL 37754.00236 5 0.20 0.211672 NaN
12 673 12610 PSO 31492.56779 5 0.15 0.176566 NaN
13 673 12610 RNS 43873.58472 5 0.20 0.245982 NaN
14 318 93790 PFL 59859.39180 2 0.20 0.285266 NaN
15 318 93790 PSO 149977.71090 2 0.15 0.714734 NaN
16 222 75250 FPE 21000.00000 6 0.10 0.100000 7525.0
17 222 75250 HFA 42000.00000 6 0.20 0.200000 15050.0
18 222 75250 PFL 42000.00000 6 0.20 0.200000 15050.0
19 222 75250 PSO 31500.00000 6 0.15 0.150000 11287.5
20 222 75250 RNS 42000.00000 6 0.20 0.200000 15050.0
21 222 75250 CRD 31500.00000 6 0.15 0.150000 11287.5
下面的信息是所需的输出-只是上述信息中前5行的剪切部分,显示了新列['Target Amount']
以及最后一行的创建-比较Ode 676时下面的词典中的6个Po中有5个。由于Ode 676缺少CRD,因此我需要一种创建行并填写信息的方法
target_dict = {"PFL":.20,"RNS":.20,"HFA":.20,"PSO":.15,"CRD":.15,"FPE":.10}
Ode Proceeds Pos Amount Positions Target Weighting Target Amt Additions
0 676 30160 FPE 51741.25000 5 0.10 0.187636 30591.22 -21150.03
1 676 30160 HFA 57299.63616 5 0.20 0.207794 61182.45 3882.81
2 676 30160 PFL 60437.40563 5 0.20 0.219173 61182.45 745.04
3 676 30160 PSO 53053.57410 5 0.15 0.192396 45886.83 -7166.74
4 676 30160 RNS 53220.36636 5 0.20 0.193001 61182.45 7962.08
5 676 30160 CRD 0 0.15 0 45886.83 45886.83
将添加CRD以完成全部6个职位,然后将基于所有['Target Amt']
加上['Amount']
的总和来计算['Proceeds']
,以得出颂德676的总数。可以算出计算量,但是我无法根据['Positions'] < 6
和Ode 676当前'target_dict'
之间的差异,想出如何在Ode中添加行['Pos']
。
答案 0 :(得分:1)
您可以将reindex
与pd.MultiIndex.from_product
一起使用,这将在'Ode'的唯一值和target_dict
的每个键之间创建所有组合,例如:
df_all = (df.set_index(['Ode', 'Pos']) #first set index for reindex them after
.reindex(pd.MultiIndex.from_product([df.Ode.unique(), target_dict.keys()],
names = ['Ode','Pos']))
.reset_index()) # index back as columns
print (df_all) #note I took rows for Ode = 676 and 953 only
Ode Pos Proceeds Amount Positions Target Weighting
0 676 PFL 30160.0 60437.40563 5.0 0.20 0.219173
1 676 RNS 30160.0 53220.36636 5.0 0.20 0.193001
2 676 HFA 30160.0 57299.63616 5.0 0.20 0.207794
3 676 PSO 30160.0 53053.57410 5.0 0.15 0.192396
4 676 CRD NaN NaN NaN NaN NaN
5 676 FPE 30160.0 51741.25000 5.0 0.10 0.187636
6 953 PFL 34960.0 8506.19390 1.0 0.20 1.000000
7 953 RNS NaN NaN NaN NaN NaN
8 953 HFA NaN NaN NaN NaN NaN
9 953 PSO NaN NaN NaN NaN NaN
10 953 CRD NaN NaN NaN NaN NaN
11 953 FPE NaN NaN NaN NaN NaN
现在要根据需要完成数据,可以尝试fillna
,map
和groupby.transform:
# fillna some columns with 0
df_all.Amount = df_all.Amount.fillna(0)
df_all.Weighting = df_all.Weighting.fillna(0)
# map the dictionary to get the values in target column
df_all.Target = df_all.Pos.map(target_dict)
# create the groupby Ode
gr = df_all.groupby('Ode')
# fill Proceeds and Positions with the first not nan value in the group
df_all.Proceeds = gr.Proceeds.transform('first')
df_all.Positions = gr.Positions.transform('first')
# create the columns Target_amt and Additions according to your equation
df_all['Target_Amt'] = (gr.Amount.transform(sum) + df_all.Proceeds)*df_all.Target
df_all['Additions'] = df_all.Amount - df_all.Target_Amt
您会得到:
print (df_all)
Ode Pos Proceeds Amount Positions Target Weighting \
0 676 PFL 30160.0 60437.40563 5.0 0.20 0.219173
1 676 RNS 30160.0 53220.36636 5.0 0.20 0.193001
2 676 HFA 30160.0 57299.63616 5.0 0.20 0.207794
3 676 PSO 30160.0 53053.57410 5.0 0.15 0.192396
4 676 CRD 30160.0 0.00000 5.0 0.15 0.000000
5 676 FPE 30160.0 51741.25000 5.0 0.10 0.187636
6 953 PFL 34960.0 8506.19390 1.0 0.20 1.000000
7 953 RNS 34960.0 0.00000 1.0 0.20 0.000000
8 953 HFA 34960.0 0.00000 1.0 0.20 0.000000
9 953 PSO 34960.0 0.00000 1.0 0.15 0.000000
10 953 CRD 34960.0 0.00000 1.0 0.15 0.000000
11 953 FPE 34960.0 0.00000 1.0 0.10 0.000000
Additions Target_Amt
0 -745.040820 61182.446450
1 -7962.080090 61182.446450
2 -3882.810290 61182.446450
3 7166.739262 45886.834837
4 -45886.834837 45886.834837
5 21150.026775 30591.223225
6 -187.044880 8693.238780
7 -8693.238780 8693.238780
8 -8693.238780 8693.238780
9 -6519.929085 6519.929085
10 -6519.929085 6519.929085
11 -4346.619390 4346.619390