在数据帧中避免KeyError

时间:2018-11-14 11:36:36

标签: python pandas dataframe

我正在使用以下代码验证数据框,

df = df[(df[['name', 'issuer_id', 'service_area_id']].notnull().all(axis=1)) &
                ((df['plan_year'].notnull()) & (df['plan_year'].astype(str).str.isdigit()) & (df['plan_year'].astype(str).str.len() == 4)) &
                (df[['network_url', 'formulary_url', 'sbc_download_url', 'treatment_cost_calculator_url']].astype(str).apply(lambda x: (x.str.contains('\A(https?:\/\/)([a-zA-Z0-9\-_])*(\.)*([a-zA-Z0-9\-]+)\.([a-zA-Z\.]{2,5})(\.*.*)?\Z')) | x.isin(['nan'])).all(axis=1)) &
                (df[['promotional_label']].astype(str).apply(lambda x: (x.str.len <= 65) | x.isin(['nan'])).all(axis=1)) &
                # (df[['sort_rank_override']].astype(str).apply(lambda x: (x.str.isdigit()) | x.isin(['nan'])).all(axis=1)) &
                ((df['hios_plan_identifier'].notnull()) & (df['hios_plan_identifier'].str.len() >= 10) & (df['hios_plan_identifier'].str.contains('\A(\d{5}[A-Z]{2}[a-zA-Z0-9]{3,7}-TMP|\d{5}[A-Z]{2}\d{3,7}(\-?\d{2})*)\Z'))) &
                (df['type'].isin(['MetalPlan', 'MedicarePlan', 'BasicHealthPlan', 'DualPlan', 'MedicaidPlan', 'ChipPlan'])) &
                (df['price_period'].isin(['Monthly', 'Yearly'])) &
                (df['is_age_29_plan'].astype(str).isin(['True', 'False', 'nan']))]
                # (df[['composite_rating']].astype(str).apply(lambda x: (x.str.isin(['True', 'False']) & x.isnotin(['nan'])).all(axis=1)))]

这会丢我

  

KeyError:“ [''name']不在索引中”

当我的数据框中不存在该列时。我需要处理所有列。如何有效地在上面的代码中添加检查,该检查仅在列存在时检查验证?

2 个答案:

答案 0 :(得分:1)

您可以使用intersection

module.exports = {
    module: {
      rules: [
        {
          test: /\.js$/,
          loader: 'ify-loader'
        }
      ]
    }
  }

编辑:

L = ['name', 'issuer_id', 'service_area_id']
cols = df.columns.intersection(L)

(df[cols].notnull().all(axis=1))

想法是首先为每个列创建有效值的字典:

df = pd.DataFrame({
        'name':list('abcdef'),
         'plan_year':[2015,2015,2015,5,5,4],
})
print (df)
  name  plan_year
0    a       2015
1    b       2015
2    c       2015
3    d          5
4    e          5
5    f          4

然后通过缺失列和assign到原始valid = {'name':'a', 'issuer_id':'a', 'service_area_id':'a', 'plan_year':2015, ...} 来过滤新字典,并创建新的DataFrame:

DataFrame

最后一个过滤器:

d1 = {k: v for k, v in valid.items() if k in set(valid.keys()) - set(df.columns)}
print (d1)
{'issuer_id': 'a', 'service_area_id': 'a'}


df1 = df.assign(**d1)
print (df1)
  name  plan_year issuer_id service_area_id
0    a       2015         a               a
1    b       2015         a               a
2    c       2015         a               a
3    d          5         a               a
4    e          5         a               a
5    f          4         a               a

最后,您可以删除帮助器列:

m1 = (df1[['name', 'issuer_id', 'service_area_id']].notnull().all(axis=1)) 
m2 = ((df1['plan_year'].notnull()) & 
      (df1['plan_year'].astype(str).str.isdigit()) & 
      (df1['plan_year'].astype(str).str.len() == 4))

df1 = df1[m1 & m2]
print (df1)
  name  plan_year issuer_id service_area_id
0    a       2015         a               a
1    b       2015         a               a
2    c       2015         a               a

答案 1 :(得分:0)

添加另一个名为columns的变量,并使用df中存在的变量对其进行过滤:

columns = ['name', 'issuer_id', 'service_area_id']
existing = [i for i in columns if i in df.columns]
df = df[(df[existing]...

编辑 您还可以将每个条件分配给变量,然后像下面这样使用它:

cond1 = df['is_age_29_plan'].astype(str).isin(['True', 'False', 'nan']) if 'is_age_29_plan' in df.columns else True

然后,在过滤语句中使用cond1