pandas 删除字符串的一部分 [英] Pandas Delete Part of String

查看:93
本文介绍了 pandas 删除字符串的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>> df
                       Time
    5/10/2017 (135) 01:05:03
    5/11/2017 (136) 04:05:06

给定一个在DataFrame中的输入日期,我该如何删除儒略日期(135)和(136),并删除中间的空格,以便输出看起来像:

Given an input date such as this in a DataFrame, how would I delete the Julian Date, (135) and (136), and remove the whitespace in the middle, so that the output looks like:

>>> df
                       Time
    5/10/2017 01:05:03
    5/11/2017 04:05:06

我尝试过:

df['Time'].replace('(135)','', regex=True, inplace=True)

输出:

>>> df
                    Time
0  5/10/2017 () 01:05:03

我想知道我在这里做错了什么.

I was wondering what I'm doing wrong here.

推荐答案

您可以使用

You can use replace by regex:

首先需要通过\转义(),因为正则表达式中的特殊字符,然后通过\d+匹配所有整数,最后通过\s*匹配)之后的零个或多个空格.

First need escape () by \ because special chars in regex, then match all ints by \d+ and last match zero or more whitespaces after ) by \s*.

df['Time'] = df['Time'].str.replace("\(\d+\)\s*", '')
print (df)
                 Time
0  5/10/2017 01:05:03
1  5/11/2017 04:05:06

如果需要转换为日期时间:

And if need convert to datetime:

df['Time'] = pd.to_datetime(df['Time'].str.replace("\(\d+\)\s*", ''))
print (df)
                 Time
0 2017-05-10 01:05:03
1 2017-05-11 04:05:06

在您的示例中,误用了转义字符\,可以代替\d+ [0-9]+:

In your sample are mising escaping chars \ and is possible use instead \d+ [0-9]+:

df['Time'].replace('\([0-9]+\)\s*','', regex=True, inplace=True)
print (df)
                 Time
0  5/10/2017 01:05:03
1  5/11/2017 04:05:06

这篇关于 pandas 删除字符串的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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