记录/捕获使用DataFrame.Apply()时抛出的异常

问题描述 投票:0回答:1

我有一个 DataFrame,其中有几列需要通过自定义函数进行转换/解析。我可以使用以下函数通过

DataFrame.Apply()
轻松完成此操作:

def process_row(row) -> dict:
   try:
       newVal = my_conversion(row['oldCol'])
       out_row= {'newCol': newVal}
   except Exception as inst:
       # Sometimes my_conversion() throws exceptions, return NaN
       out_row = {'newCol': Nan}
       # Print error, but can we capture it???
       print(repr(inst))
   return out_row

这将返回一个漂亮的新字典,其中包含已处理行的新列名称和值。然后我可以将转换函数应用到我的 DataFrame,如下所示:

a = df.apply(lambda x: process_row(x), axis=1, result_type="expand"
df1 = df.merge(a, left_index=True, right_index=True)

结果完美。我得到了原始数据框,其中有一个名为“newCol”的新列,其中包含每行的转换值。

有时,如果数据有效,转换可能会引发异常。错误会打印到屏幕上,并且转换后的值会记录为 NaN。从 DataFrame 的角度来看,任务已完成。

但是,我想在单独的数据结构中捕获这些错误,而不是仅仅打印它们。如何捕获一些错误信息并从 lambda 函数返回它,以便我可以将其存储在自己的数据帧(或列表)中? (我见过的所有多个返回值问题都是将返回的值添加到原始DataFrame。我不想将错误添加到原始DataFrame)

python-3.x pandas dataframe exception apply
1个回答
0
投票

将列表或字典传递给您的函数以保存异常:

def process_row(row, ex=None) -> dict:
    try:
        newVal = int(row['oldCol'])
        out_row= {'newCol': newVal}
    except Exception as inst:
        # Sometimes my_conversion() throws exceptions, return NaN
        out_row = {'newCol': float('nan')}
        # save exception to the list
        if isinstance(ex, list):
            ex.append(inst)
        elif isinstance(ex, dict):
            ex[row.name] = inst
    return out_row

df = pd.DataFrame({'oldCol': [1,2,'a',None]})

lst = []
out = df.apply(process_row, axis=1, result_type="expand", ex=lst)
print(out)
print(lst)

输出:

# out
  newCol
0     1.0
1     2.0
2     NaN
3     NaN

# lst
[ValueError("invalid literal for int() with base 10: 'a'"),
 TypeError("int() argument must be a string, a bytes-like object or a real number, not 'NoneType'")]

带字典:

dic = {}
out = df.apply(process_row, axis=1, result_type="expand", ex=lst)
print(dic)

{2: ValueError("invalid literal for int() with base 10: 'a'"),
 3: TypeError("int() argument must be a string, a bytes-like object or a real number, not 'NoneType'")}
© www.soinside.com 2019 - 2024. All rights reserved.