我有2个数据帧,如下所示。我需要一个结果数据,该数据的数据帧1的周期总和除以python在引脚,站点和部门索引的数据帧2的周期总和。
Pin Site Department Period1 Period2 Period3 Period4
1001 L 42 1 0 2 3
1003 L 42 4 4 3 4
1002 R 45 4 5 2 4
Pin Site Department Period1 Period2 Period3 Period4
1002 R 45 5 6 5 5
1003 L 42 4 5 6 8
1001 L 42 1 2 4 5
Pin Site Department Period1 Period2 Period3 Period4
1001 L 42 1/1 (1+0)/(1+2) (1+0+2)/(1+2+4) (1+0+2+3)/(1+2+4+5)
1002 R 45 4/5 (4+5)/(5+6) (4+5+2)/(5+6+5) (4+5+2+4)/(5+6+5+5)
1003 L 42 4/4 (4+4)/(4+5) (4+4+3)/(4+5+6) (4+4+3+4)/(4+5+6+8)
无论两个数据帧中的引脚顺序如何,我都需要如上所述的结果数据帧。期间数将逐月增加。
我认为需要set_index
在部门对齐指数由div
用cumsum
,最后添加reset_index
从列MultiIndex
:
df11 = df1.set_index(['Pin','Site','Department']).cumsum(axis=1)
df22 = df2.set_index(['Pin','Site','Department']).cumsum(axis=1)
df = df11.div(df22).reset_index()
print (df)
Pin Site Department Period1 Period2 Period3 Period4
0 1001 L 42 1.0 0.333333 0.428571 0.500000
1 1002 R 45 0.8 0.818182 0.687500 0.714286
2 1003 L 42 1.0 0.888889 0.733333 0.652174
编辑:
对于筛选出的值,>1
需要反转条件-保持alues-<=1
比较并检查每行是否s:all
True
df = df11.div(df22)
df = df[(df <= 1).all(axis=1)].reset_index()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句