我有一个嵌套的字典,其中我要深入三层,以根据数据框中与列匹配的键提取一个值。下面是我正在使用的代码。
columns = df.columns
for column in columns:
for k1, v1 in model_4_factors.items():
for k2, v2 in v1.items():
for k3, v3 in v2.items():
#print(k3)
if k2 == df['RATING_CLASS_CODE'] and k3 == df['unit_value_factors']:
print(k3)
我一直收到此值错误,但我不确定为什么:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-258-ab68e28884a4> in <module>
5 for k3, v3 in v2.items():
6 #print(k3)
----> 7 if k2 == df['RATING_CLASS_CODE'] and k3 == df['unit_value_factors']:
8 print(k3)
C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1574 raise ValueError("The truth value of a {0} is ambiguous. "
1575 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
-> 1576 .format(self.__class__.__name__))
1577
1578 __bool__ = __nonzero__
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
我尝试使用iteritems()
,它告诉我该字典没有功能iteritems()
。当我取出if ==
语句并让python打印不同的k和v电平时,它可以工作,所以我知道嵌套循环是正确的。有想法吗?
答案 0 :(得分:0)
我知道了。我切换到iterrow
语句。但是它非常慢。
for index, row in df.iterrows():
for k1, v1 in model_4_factors.items():
for k2, v2 in v1.items():
if isinstance(v2, dict):
for k3, v3 in v2.items():
if k2 == row['RATING_CLASS_CODE'] and k3 == row['unit_value_model']:
df['value_factor_4'] = v3
elif k2 == row['RATING_CLASS_CODE'] and k3 == row['MVEH_CC_Model']:
df['cc_factor_4'] = v3
elif k2 == row['term_model'] and k3 == row['advanced_purchase_days_model']:
df['advanced_days_factor_4'] = v3
elif k2 == row['marital_status_model'] and k3 == row['Driver_Age_model']:
df['driver_age_factor_4'] = v3
else:
df['value_factor_4'] = None
else:
if row['mveh_pkg_typ_cd'] == k2:
df['mveh_pkg_typ_cd_factor_4'] = v2
elif row['CLded'] == k2:
df['CLded_factor_4'] = v2
elif row['unit_drv_exp_model'] == k2:
df['unit_drv_exp_model'] = v2
elif row['v_age_model'] == k2:
df['v_age_model_factor_4'] = v2
elif row['years_owned_model'] == k2:
df['years_owned_model_factor_4'] = v2
elif row['FIN_RESP_CD'] == k2:
df['FIN_RESP_CD_factor_4'] = v2
elif row['st_ad_moto_safety_course'] == k2:
df['st_ad_moto_safety_course_factor_4'] = v2
elif row['st_ai_instructor_course'] == k2:
df['st_ai_instructor_course_factor_4'] = v2
elif row['st_ci_instructor_course'] == k2:
df['st_ci_instructor_course_factor_4'] = v2
elif row['st_mf_moto_safety_course '] == k2:
df['st_mf_moto_safety_course_factor_4'] = v2
elif row['st_mi_instructor_course'] == k2:
df['st_mi_instructor_course_factor_4'] = v2
elif row['st_mc_moto_safety_course'] == k2:
df['st_mc_moto_safety_course_factor_4'] = v2
elif row['st_rc_moto_safety_course'] == k2:
df['st_rc_moto_safety_course_factor_4'] = v2
elif row['st_ri_instructor_course'] == k2:
df['st_ri_instructor_course_factor_4'] = v2
elif row['ds_pn_prior_insurance'] == k2:
df['ds_pn_prior_insurance_factor_4'] = v2
elif row['st_rc_moto_safety_course'] == k2:
df['st_rc_moto_safety_course_factor_4'] = v2
elif row['Loyalty'] == k2:
df['Loyalty_factor_4'] = v2
elif row['ds_mc_motorcycle_endorsement'] == k2:
df['ds_mc_motorcycle_endorsement_factor_4'] = v2
elif row['multi_unit_model2'] == k2:
df['multi_unit_model2_factor_4'] = v2
elif row['ds_ad_affinity'] == k2:
df['ds_ad_affinity_factor_4'] = v2
elif row['ds_ak_alliance'] == k2:
df['ds_ak_alliance_factor_4'] = v2
elif row['multi_policy_count_model'] == k2:
df['multi_policy_count_model_factor_4'] = v2
elif row['ds_fp_paid_in_full'] == k2:
df['ds_fp_paid_in_full_factor_4'] = v2
else:
pass