• <tfoot id='hPFNa'></tfoot>

      <legend id='hPFNa'><style id='hPFNa'><dir id='hPFNa'><q id='hPFNa'></q></dir></style></legend>

      <small id='hPFNa'></small><noframes id='hPFNa'>

        <bdo id='hPFNa'></bdo><ul id='hPFNa'></ul>

        <i id='hPFNa'><tr id='hPFNa'><dt id='hPFNa'><q id='hPFNa'><span id='hPFNa'><b id='hPFNa'><form id='hPFNa'><ins id='hPFNa'></ins><ul id='hPFNa'></ul><sub id='hPFNa'></sub></form><legend id='hPFNa'></legend><bdo id='hPFNa'><pre id='hPFNa'><center id='hPFNa'></center></pre></bdo></b><th id='hPFNa'></th></span></q></dt></tr></i><div id='hPFNa'><tfoot id='hPFNa'></tfoot><dl id='hPFNa'><fieldset id='hPFNa'></fieldset></dl></div>

      1. 如何在 pandas 数据框中使用 ast.literal_eval 并处理异常

        时间:2023-09-02
        <i id='prlqO'><tr id='prlqO'><dt id='prlqO'><q id='prlqO'><span id='prlqO'><b id='prlqO'><form id='prlqO'><ins id='prlqO'></ins><ul id='prlqO'></ul><sub id='prlqO'></sub></form><legend id='prlqO'></legend><bdo id='prlqO'><pre id='prlqO'><center id='prlqO'></center></pre></bdo></b><th id='prlqO'></th></span></q></dt></tr></i><div id='prlqO'><tfoot id='prlqO'></tfoot><dl id='prlqO'><fieldset id='prlqO'></fieldset></dl></div>

      2. <tfoot id='prlqO'></tfoot>
        <legend id='prlqO'><style id='prlqO'><dir id='prlqO'><q id='prlqO'></q></dir></style></legend>

            • <bdo id='prlqO'></bdo><ul id='prlqO'></ul>
                <tbody id='prlqO'></tbody>

                <small id='prlqO'></small><noframes id='prlqO'>

                1. 本文介绍了如何在 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 并处理异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在python列表中交换元组/列表中的值? 下一篇:str.startswith 是如何真正起作用的?

                  相关文章

                  <tfoot id='HIEnU'></tfoot>
                  <i id='HIEnU'><tr id='HIEnU'><dt id='HIEnU'><q id='HIEnU'><span id='HIEnU'><b id='HIEnU'><form id='HIEnU'><ins id='HIEnU'></ins><ul id='HIEnU'></ul><sub id='HIEnU'></sub></form><legend id='HIEnU'></legend><bdo id='HIEnU'><pre id='HIEnU'><center id='HIEnU'></center></pre></bdo></b><th id='HIEnU'></th></span></q></dt></tr></i><div id='HIEnU'><tfoot id='HIEnU'></tfoot><dl id='HIEnU'><fieldset id='HIEnU'></fieldset></dl></div>

                  1. <small id='HIEnU'></small><noframes id='HIEnU'>

                    1. <legend id='HIEnU'><style id='HIEnU'><dir id='HIEnU'><q id='HIEnU'></q></dir></style></legend>
                        <bdo id='HIEnU'></bdo><ul id='HIEnU'></ul>