gpt4 book ai didi

python - 从数据框中的每一行中删除日期时间字符串

转载 作者:行者123 更新时间:2023-12-02 18:54:08 24 4
gpt4 key购买 nike

我有一个如下所示的数据框:

   Film      Description       
0 Batman Viewed in 2021-10-04T14:30:31Z City Hall, London
1 Superman Aired 2012-01-04R11:01:10Z in the USA first
2 Hulk 2010-07-04S07:22:02Z Still being produced

我想从“描述”列中的每一行中删除日期时间,如下所示:

    Film      Description      
0 Batman Viewed in City Hall, London
1 Superman Aired in the USA first
2 Hulk Still being produced

我尝试过这个字符串正则表达式:

df['Description'] = df['Description '].str.replace(r'\^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})Z', '')

最佳答案

\^ 匹配插入符号。

除了 T 之外,我在日期时间戳中看到 RS,必须添加它们。

使用

\s*\b\d{4}-\d{2}-\d{2}[TRS]\d{2}:\d{2}:\d{2}Z\b

参见proof .

说明

--------------------------------------------------------------------------------
\s* whitespace (\n, \r, \t, \f, and " ") (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
\d{2} digits (0-9) (2 times)
--------------------------------------------------------------------------------
- '-'
--------------------------------------------------------------------------------
\d{2} digits (0-9) (2 times)
--------------------------------------------------------------------------------
[TRS] any character of: 'T', 'R', 'S'
--------------------------------------------------------------------------------
\d{2} digits (0-9) (2 times)
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\d{2} digits (0-9) (2 times)
--------------------------------------------------------------------------------
: ':'
--------------------------------------------------------------------------------
\d{2} digits (0-9) (2 times)
--------------------------------------------------------------------------------
Z 'Z'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char

关于python - 从数据框中的每一行中删除日期时间字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66414808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com