我有一个数据框,
ip_df:
name class sec details
0 tom I a [{'class':'I','sec':'a','subjects':['numbers','ethics']},{'class':'I','sec':'b','subjects':['numbers','moral-science']},{'class':'I','sec':'c','subjects':['moral-science','ethics']},{'class':'I','subjects':['numbers','ethics1']}]
1 sam I d [{'class':'I','sec':'a','subjects':['numbers','ethics']},{'class':'I','sec':'b','subjects':['numbers','moral-science']},{'class':'I','sec':'c','subjects':['moral-science','ethics']},{'class':'I','subjects':['numbers','ethics1']}]
,结果数据帧应该是
op_df:
name class sec subjects
0 tom I a ['numbers','ethics']
1 sam I d ['numbers','ethics1']
“ op_df”必须根据以下条件进行构图,
答案 0 :(得分:2)
如果两个条件都需要首先匹配值的解决方案,请使用next
和iter
技巧,如果不匹配,则添加默认值[0, 0]
:
final = []
for a, b, c in zip(df['class'], df['sec'], df['details']):
out = []
for x in c:
m1 = x['class'] == a
if m1 and x.get('sec') == b:
out.append(x['subjects'])
elif m1 and 'sec' not in list(x.keys()):
out.append(x['subjects'])
final.append(next(iter(out), [0,0]))
df['subjects'] = final