df1
df1 = pd.DataFrame({ 'group' : ["A","A","A","B","B","B"],
'par' : [5,5,15,10,2,11],
'val' :[50,10,180,10,10,660],
'set_0' :["Country","Country","Country","Country","Country","Country"],
'set_1' :["size1","size1","size2","size3","size4","size3"],
'set_2' :["size12","size12","size12","size9","size13","size13"],
'set_3' :["size14","size14","size15","NO","NO","NO"],
'set_4' :["NO","NO","NO","size25","size25","size27"],
'set_5' :["NO","NO","NO","NO","NO","NO"]
})
df2
df2 = pd.DataFrame({ 'group' : ["A","A","A","A","A","A","B","B","B","B","B","B","B"],
'set' : ["Country","size1","size2","size12","size14","size15","Country","size3","size4","size9","size13","size25","size27"],
})
对于df2的每一行(X组组合组合),我想应用一个函数进行计算(“ val”之和/“ par”之和)。
我尝试将某些东西与apply函数一起使用,但是由于我在python中还很新,所以我无法真正弄清楚。 有人可以帮忙解决问题吗?
以下是预期结果:
随着图片上传不断失败,我还分享了以下代码,以最丑陋且最无效的硬编码方式获取结果:
a1=df1[(df1["group"]==df2.iloc[0,0])&(df1["set_0"]==df2.iloc[0,1])].sum()["val"]
a2=df1[(df1["group"]==df2.iloc[0,0])&(df1["set_0"]==df2.iloc[0,1])].sum()["par"]
b1=df1[(df1["group"]==df2.iloc[1,0])&(df1["set_1"]==df2.iloc[1,1])].sum()["val"]
b2=df1[(df1["group"]==df2.iloc[1,0])&(df1["set_1"]==df2.iloc[1,1])].sum()["par"]
c1=df1[(df1["group"]==df2.iloc[2,0])&(df1["set_1"]==df2.iloc[2,1])].sum()["val"]
c2=df1[(df1["group"]==df2.iloc[2,0])&(df1["set_1"]==df2.iloc[2,1])].sum()["par"]
d1=df1[(df1["group"]==df2.iloc[3,0])&(df1["set_2"]==df2.iloc[3,1])].sum()["val"]
d2=df1[(df1["group"]==df2.iloc[3,0])&(df1["set_2"]==df2.iloc[3,1])].sum()["par"]
e1=df1[(df1["group"]==df2.iloc[4,0])&(df1["set_3"]==df2.iloc[4,1])].sum()["val"]
e2=df1[(df1["group"]==df2.iloc[4,0])&(df1["set_3"]==df2.iloc[4,1])].sum()["par"]
f1=df1[(df1["group"]==df2.iloc[5,0])&(df1["set_3"]==df2.iloc[5,1])].sum()["val"]
f2=df1[(df1["group"]==df2.iloc[5,0])&(df1["set_3"]==df2.iloc[5,1])].sum()["par"]
g1=df1[(df1["group"]==df2.iloc[6,0])&(df1["set_0"]==df2.iloc[6,1])].sum()["val"]
g2=df1[(df1["group"]==df2.iloc[6,0])&(df1["set_0"]==df2.iloc[6,1])].sum()["par"]
h1=df1[(df1["group"]==df2.iloc[7,0])&(df1["set_1"]==df2.iloc[7,1])].sum()["val"]
h2=df1[(df1["group"]==df2.iloc[7,0])&(df1["set_1"]==df2.iloc[7,1])].sum()["par"]
j1=df1[(df1["group"]==df2.iloc[8,0])&(df1["set_1"]==df2.iloc[8,1])].sum()["val"]
j2=df1[(df1["group"]==df2.iloc[8,0])&(df1["set_1"]==df2.iloc[8,1])].sum()["par"]
k1=df1[(df1["group"]==df2.iloc[9,0])&(df1["set_2"]==df2.iloc[9,1])].sum()["val"]
k2=df1[(df1["group"]==df2.iloc[9,0])&(df1["set_2"]==df2.iloc[9,1])].sum()["par"]
l1=df1[(df1["group"]==df2.iloc[10,0])&(df1["set_2"]==df2.iloc[10,1])].sum()["val"]
l2=df1[(df1["group"]==df2.iloc[10,0])&(df1["set_2"]==df2.iloc[10,1])].sum()["par"]
m1=df1[(df1["group"]==df2.iloc[11,0])&(df1["set_4"]==df2.iloc[11,1])].sum()["val"]
m2=df1[(df1["group"]==df2.iloc[11,0])&(df1["set_4"]==df2.iloc[11,1])].sum()["par"]
n1=df1[(df1["group"]==df2.iloc[12,0])&(df1["set_4"]==df2.iloc[12,1])].sum()["val"]
n2=df1[(df1["group"]==df2.iloc[12,0])&(df1["set_4"]==df2.iloc[12,1])].sum()["par"]
到目前为止,我必须为每个元素手动创建“ val”和“ par”对。 然后,
a=a1/a2
b=b1/b2
c=c1/c2
d=d1/d2
e=e1/e2
f=f1/f2
g=g1/g2
h=h1/h2
j=j1/j2
k=k1/k2
l=l1/l2
m=m1/m2
n=n1/n2
最后,结果是:
df2["desired_calculation"]=[a,b,c,d,e,f,g,h,j,k,l,m,n]