[英]Replace values in second df determined by first df - pandas
我有两个单独的数据框,第一个本质上是对值的描述,第二个包含实际值。 我希望根据第一个 df 中的某个分组来乘以第二个 df 中的值。
具体来说,对于每个独特的Time
,我对GR1
的 Direction 感兴趣,它是参考组。 如果GR1
== Group
& Direction
== down
,则将df2
中'Val1','Val2'
每一行乘以 -1 以获得该唯一Time
。
对于Time
点1
, Group A
已Up
,因此df2
原样。 对于Time
点2
, Group A
为Down
,因此我想将df2
中与列'Val1','Val2'
的Group A
Group B
组关联的行乘以 -1。 对于剩余的唯一Time
点,此模式重复。
import pandas as pd
df = pd.DataFrame({
'Time' : [1,1,2,2,3,3,4,4],
'GR1' : ['A','A','A','A','A','A','A','A'],
'GR2' : ['B','B','B','B','B','B','B','B'],
'Group' : ['A','B','A','B','A','B','A','B'],
'Direction' : ['Up','Down','Down','Up','Up','Down','Down','Up'],
})
df2 = pd.DataFrame({
'Time' : [1,1,1,2,2,2,3,3,3,4,4,4],
'Group' : ['A','B','A','A','B','A','A','B','A','A','B','A'],
'Item' : ['Red','Blue','Orange','Red','Blue','Orange','Red','Blue','Orange','Red','Blue','Orange'],
'Val1' : [-2.0,-1.0,-1.0,0.0,0.0,1.0,0.0,1.0,2.0,-1.0,0.0,0.0],
'Val2' : [-2.0,-1.0,-2.0,-1.0,-2.0,-1.0,0.0,1.0,3.0,-2.0,-1.0,-2.0],
})
df.loc[(df['GR1'] == df['Group']) & (df['Direction'] == 'Down'), df2['Val1','Val2']] * -1
预期 output:
Time Item Val1 Val2
0 1 Red -2.0 -2.0
1 1 Blue -1.0 -1.0
2 1 Orange -1.0 -2.0
3 2 Red 0.0 1.0 * Multiply by -1
4 2 Blue 0.0 2.0 * Multiply by -1
5 2 Orange -1.0 1.0 * Multiply by -1
6 3 Red 0.0 0.0
7 3 Blue 1.0 1.0
8 3 Orange 2.0 3.0
9 4 Red 1.0 2.0 * Multiply by -1
10 4 Blue 0.0 1.0 * Multiply by -1
11 4 Orange 0.0 2.0 * Multiply by -1
尝试:
q = 'GR1 == Group & Direction == "Down"'
mask = df2.Time.isin(df.query(q).Time)
df2.loc[mask, ['Val1', 'Val2']] *= -1
df2
Time Group Item Val1 Val2
0 1 A Red -2.0 -2.0
1 1 B Blue -1.0 -1.0
2 1 A Orange -1.0 -2.0
3 2 A Red -0.0 1.0
4 2 B Blue -0.0 2.0
5 2 A Orange -1.0 1.0
6 3 A Red 0.0 0.0
7 3 B Blue 1.0 1.0
8 3 A Orange 2.0 3.0
9 4 A Red 1.0 2.0
10 4 B Blue -0.0 1.0
11 4 A Orange -0.0 2.0
这是一种从您的原始解决方案工作的方法:
times = df.loc[(df['GR1'] == df['Group']) & (df['Direction'] == 'Down')]['Time']
df2.loc[df2['Time'].isin(times), ['Val1','Val2']] *= -1
Output:
Time Group Item Val1 Val2
0 1 A Red -2.0 -2.0
1 1 B Blue -1.0 -1.0
2 1 A Orange -1.0 -2.0
3 2 A Red -0.0 1.0
4 2 B Blue -0.0 2.0
5 2 A Orange -1.0 1.0
6 3 A Red 0.0 0.0
7 3 B Blue 1.0 1.0
8 3 A Orange 2.0 3.0
9 4 A Red 1.0 2.0
10 4 B Blue -0.0 1.0
11 4 A Orange -0.0 2.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.