如何在 pandas 数据框中使用 ast.literal_eval 并处理异常 [英] How to use ast.literal_eval in a pandas dataframe and handle exceptions

查看:19
本文介绍了如何在 pandas 数据框中使用 ast.literal_eval 并处理异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 dataframe,其中有一列包含 tuple 数据作为字符串.例如.'(5,6)'.我需要将其转换为元组结构.一种方法是使用 ast.literal_eval().我就是这样用的.

I have a dataframe with a column containing a tuple data as a string. Eg. '(5,6)'. I need to convert this to a tuple structure. One way of doing it is using the ast.literal_eval(). I am using it in this way.

df['Column'] = df['Column'].apply(ast.literal_eval)

很遗憾,我在此列中的数据也包含空字符串.ast.literal_eval() 无法处理此问题.我收到此错误.

Unfortunately, my data in this column contains empty strings also. The ast.literal_eval() is not able to handle this. I get this error.

SyntaxError: 解析时出现意外 EOF

我不确定这是否是因为它无法处理这样的字符.根据我的阅读,我发现 ast.literal_eval() 仅适用于字符串结构中存在列表、字典或元组的情况.

I am unsure if this is because it is unable to handle such a character. Based on my reading, I found that ast.literal_eval() works only in cases when a list, dict or tuple is there inside a string structure.

为了克服这个问题,我尝试创建自己的函数并在引发异常时返回一个空字符串.

To overcome this I tried to create my own function and return an empty string if it raises an exception.

def literal_return(val):
    try:
        return ast.literal_eval(val)
    except ValueError:
        return (val)

df['Column2'] = df['Column'].apply(literal_return)

即使在这种情况下,也会弹出相同的错误.我们如何处理这个.即使有一种方法可以忽略某些行来应用该函数并应用于其余行,那也会很棒.任何帮助表示赞赏.

Even in this case, the same error pops up. How do we handle this. It would be great even if there is a way to ignore certain rows to apply the function and apply on the rest. Any help is appreciated.

推荐答案

我会这样做,只需要每个条目的字符串类型:

I would do it simply requiring a string type from each entry:

from ast import literal_eval
df['column_2'] = df.column_1.apply(lambda x: literal_eval(str(x)))

如果您需要高级异常处理,您可以这样做,例如:

If You need to advanced Exception handling, You could do, for example:

def f(x):
    try:
        return literal_eval(str(x))   
    except Exception as e:
        print(e)
        return []

df['column_2'] = df.column_1.apply(lambda x: f(x))   

这篇关于如何在 pandas 数据框中使用 ast.literal_eval 并处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆