需要检查bool3_res
的'detected'
键和{bool3_res
键
如果bool3_res['detected'] == bool1_res['is_doc1'] == True
,那么我的resp
必须返回
如果bool3_res['detected'] == bool2_res['is_doc1'] == True
,那么我的resp
必须返回\
3:否则返回“无效”
数据帧
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}"
我的代码
^{2}$我的预期结果
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'}}}"
我的出局 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
您应该避免使用^{} ,而是使用^{} 和}来处理每行,对于一个元素列表,将}、}:
x
代替{[]
添加到{x['bool2']
和{^{pr2}$
我假设这就是扩展代码行后数据的样子:(而且,如果您可以添加一些空格…^^ ^)
我的回答
执行分为两部分:(1)解析字符串;(2)处理/生成“新”列值。在
^{pr2}$第1部分:解析dict字符串
此函数通过pd.DataFrame.applymap应用于dataframe中的每个元素,并使用
ast.literal_eval
,正如@jezrael正确地建议的那样。在第2部分:处理数据(即制作“新”列)
此函数应用于数据帧的每一行(由pd.DataFrame.agg):
根据你发布函数中的逻辑,我:
检查
bool3['detected']
是否为False(前两个条件都检测到==True);如果是,则引发ValueError检查bool1的is_doc1是否为True,如果不是,则为bool2
如果两种情况都不是真的,则引发
ValueError
要执行,请运行:
请注意,这将解析dataframe中的所有元素。在
要只解析列
bool_res
列,可以分两步执行:结果
相关问题 更多 >
编程相关推荐