Reputation: 632
df1:
df1=pd.DataFrame({'id':['val1','val2','val3','val4','val5','val6'],
'min':['10','10','75','42','20','50'],
'max':['93','43','122','80','30','105']})
df2:
df2=pd.DataFrame({'id':['val1','val2','val5','val1','val5','val2'],
'check':['55.4','35.8','93','11.5','23.8','3.22']})
The objective is to sum the corresponding check column values in df2 when id matches with df1 and check if the resultant sum is within min-max range in df1 and update values in result column of df2.
Output df:
id check result
val1 55.4 positive
val2 35.8 positive
val5 93 positive
val3 10.1 negative
val1 11.5 positive
val5 23.8 positive
val2 3.22 positive
Many thanks!
Upvotes: 1
Views: 67
Reputation: 30920
I think you need DataFrame.merge
with GroupBy.transform
. Then create a new column with np.where
:
df3 = df2.merge(df1, how='left', on = 'id')
s = df3.groupby('id')['check'].transform('sum')
df2['result']=np.where(s.lt(df3['max']) & s.gt(df3['min']), 'positive', 'negative')
print(df2)
Output df2
id check result
0 val1 55.4 positive
1 val2 35.8 positive
2 val5 93 negative
3 val1 11.5 positive
4 val5 23.8 negative
5 val2 3.22 positive
Upvotes: 2
Reputation: 150745
We can merge and use between
:
(df2.merge(df1, on='id', how='left')
.assign(result=lambda x: np.where(x.check.between(x['min'],x['max']),
'positive', 'negative')
)
.drop(['min','max'], axis=1)
)
Output:
id check result
0 val1 55.4 positive
1 val2 35.8 positive
2 val5 93 negative
3 val1 11.5 positive
4 val5 23.8 positive
5 val2 3.22 positive
Upvotes: 2
Reputation: 323286
Let us do merge
, the eval
df=df2.merge(df1,how='left').eval('result=check>min and check < max')
Out[621]:
id check min max result
0 val1 55.4 10 93 True
1 val2 35.8 10 43 True
2 val5 93 20 30 False
3 val1 11.5 10 93 True
4 val5 23.8 20 30 True
5 val2 3.22 10 43 True
Upvotes: 3