本文介绍了如何在 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: unexpected EOF while parsing

我不确定这是否是因为它无法处理此类字符.根据我的阅读,我发现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并处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 08:38