我有两个数据框:
df1 = pd.DataFrame({ 'pressure' : [42,42,42,42,42,42,42,36,36,36,36,36,36,36],
'load' : [350,350,350,350,350,350,350,700,700,700,700,700,700,700],
'speed' : [70,60,50,40,30,20,10,70,60,50,40,30,20,10],
'lforce' : [3.6,3.5,3.3,3.2,3.1,3.1,2.9,7.7,7.3,7.0,6.8,6.5,6.4,6.1],
'rforce' : [3.4,3.2,3.1,3.0,2.9,2.8,2.7,7.6,7.2,6.9,6.6,6.3,6.2,5.9]
}).set_index(['pressure','load','speed'])
df2 = pd.DataFrame({ 'pressure' : [47,47,47,47,47,47,47],
'load' : [20,20,20,20,20,20,20],
'speed' : [70,60,50,40,30,20,10],
'lforce' : [2.5,2.1,1.9,1.7,1.5,1.3,1.2],
'rforce' : [2.8,2.6,2.4,2.2,2.0,1.8,1.7]
}).set_index(['pressure','load','speed'])
格式化:
>>> df1
lforce rforce
pressure load speed
42 350 70 3.6 3.4
60 3.5 3.2
50 3.3 3.1
40 3.2 3.0
30 3.1 2.9
20 3.1 2.8
10 2.9 2.7
36 700 70 7.7 7.6
60 7.3 7.2
50 7.0 6.9
40 6.8 6.6
30 6.5 6.3
20 6.4 6.2
10 6.1 5.9
>>> df2
lforce rforce
pressure load speed
47 20 70 2.5 2.8
60 2.1 2.6
50 1.9 2.4
40 1.7 2.2
30 1.5 2.0
20 1.3 1.8
10 1.2 1.7
我想从每个df2
的{{1}}和df1
列的lforce
中减去rforce
,以获得结果数据框{{1} }。
我的问题是我需要在减法期间忽略speed
中的df3
和pressure
,但保留load
的原件。
期望的结果:
df2
答案 0 :(得分:4)
df1.sub(df2.reset_index([0, 1], drop=True), level=2)
输出:
lforce rforce
pressure load speed
42 350 70 1.1 0.6
60 1.4 0.6
50 1.4 0.7
40 1.5 0.8
30 1.6 0.9
20 1.8 1.0
10 1.7 1.0
36 700 70 5.2 4.8
60 5.2 4.6
50 5.1 4.5
40 5.1 4.4
30 5.0 4.3
20 5.1 4.4
10 4.9 4.2
答案 1 :(得分:1)
可能是这样的:
>>> df3 = df1.reset_index(level=[0,1])
>>> df4 = df2.reset_index(level=[0,1])
>>> df4['pressure'] = 0
>>> df4['load'] = 0
>>> df3 - df4
pressure load lforce rforce
speed
10 42 350 1.7 1.0
10 36 700 4.9 4.2
20 42 350 1.8 1.0
20 36 700 5.1 4.4
30 42 350 1.6 0.9
30 36 700 5.0 4.3
40 42 350 1.5 0.8
40 36 700 5.1 4.4
50 42 350 1.4 0.7
50 36 700 5.1 4.5
60 42 350 1.4 0.6
60 36 700 5.2 4.6
70 42 350 1.1 0.6
70 36 700 5.2 4.8
现在您只需将pressure
和load
移回索引
答案 2 :(得分:0)
这是你要找的吗?
d1 = df1.reset_index(['pressure','load'])
d2 = df2.reset_index(['pressure','load'])
r0 = d1.merge(d2, left_index=True, right_index=True)
r1 = r0.set_index(['pressure_x','load_x'], drop=False)
r1['lforce'] = r1.lforce_x - r1.lforce_y
r1['rforce'] = r1.rforce_x - r1.rforce_y
df3 = r1[['lforce','rforce']]
df3