代码之家  ›  专栏  ›  技术社区  ›  newleaf

pandas dataframe apply函数值错误

  •  0
  • newleaf  · 技术社区  · 4 年前

    有多个表需要处理。每个表中有一列与日期相关,但日期格式正在更改,如我使用的5/26、05/26、05/26/20200526205205262020

      df[date] = df[date].apply(dateutil.parser.parse, dayfirst=dayfirst,
                                                             yearfirst=yearfirst)
    

    它以前工作得很好,但是最近日期列中的一些表可能有类似的字符串 "unknown" "missing" other strings . 然后我得到一个错误,它破坏了程序。

     "ValueError: Unknown string format"
    

    如何处理它来排除我得到的行

    "ValueError: Unknown string format"
    

    谢谢。

    0 回复  |  直到 4 年前
        1
  •  0
  •   newleaf    4 年前

      df=df[df["date"].str.contains(re.compile('\d+'))]
      df[date] = df[date].apply(dateutil.parser.parse, dayfirst=dayfirst,
                                                         yearfirst=yearfirst)