Need to check the 'detected'
key of bool3_res
with key is_doc1
of bool_res
and bool_2
res
-
if
bool3_res['detected'] == bool1_res['is_doc1'] == True
then myresp
has to return -
if
bool3_res['detected'] == bool2_res['is_doc1'] == True
then myresp
has to return\
3: else return 'Not valid'
Data frame
user_uid,bool1,bool2,bool3,bool1_res,bool2_res,bool3_res
1001,27452.webp,981.webp,d92e.webp,"{'is_doc1': False, 'is_doc2': True}","{'is_doc1': True, 'is_doc2': True}","{'detected': True, 'count': 1}"
1002,27452.webp,981.webp,d92e.webp,"{'is_doc1': True, 'is_doc2': True}","{'is_doc1': False, 'is_doc2': True}","{'detected': True, 'count': 1}"
My code
def new_func(x):
d1 = df['bool1_res'].to_dict()
d1 = eval(d1[0])
d2 = df['bool2_res'].to_dict()
d2 = eval(d2[0])
d3 = df['bool3_res'].to_dict()
d3 = eval(d3[0])
if d1['is_doc1'] == d3['detected'] == True:
resp = {
"task_id": "uid",
"group_id": "uid",
"data": {
"document1": df['bool1'],
"document2": df['bool3']
}
}
elif d2['is_doc1'] == d3['detected'] == True:
resp = {
"task_id": "user_uid",
"group_id": "uid",
"data": {
"document1": df['bool2'],
"document2": df['bool3']
}
}
elif d3['detected'] == False:
resp = 'Not valid'
else:
resp = 'Not valid'
return resp
df['new'] = df.apply(new_func, axis = 1)
#df['new'] = df[['bool1', 'bool2', 'bool3', 'bool1_res', 'bool2_res', 'bool3_res']].applymap(new_func)
My expected out
df['new']
{'u_id': 'uid', 'group': 'uid', 'data': {'document1': ['981.webp'], 'document2': {'d92e.webp'}}}"
{'u_id': 'uid', 'group': 'uid', 'data': {'document1': ['27452.webp'], 'document2': {'d92e.webp'}}}"
My Out df['new']
0 {'task_id': 'user_uid', 'group_id': 'uid', 'data': {'document1': ['981.webp', '981.webp'], 'document2': ['d92e.webp', 'd92e.webp']}}
1 {'task_id': 'user_uid', 'group_id': 'uid', 'data': {'document1': ['981.webp', '981.webp'], 'document2': ['d92e.webp', 'd92e.webp']}}
Name: new, dtype: object
from How to apply if condtion and apply to dataframe
No comments:
Post a Comment