如何在数据框列中删除字符串的子字符串? [英] How to Remove a Substring of String in a Dataframe Column?

查看:67
本文介绍了如何在数据框列中删除字符串的子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简化的数据框:

I have this simplified dataframe:

ID, Date
1 8/24/1995
2 8/1/1899 :00

我如何使用熊猫的力量来识别日期具有额外:00 的数据框并将其删除。

How can I use the power of pandas to recognize any date in the dataframe which has extra :00 and removes it.

有什么办法解决这个问题吗?

Any idea how to solve this problem?

我尝试过这种语法,但没有帮助:

I have tried this syntax but did not help:

df[df["Date"].str.replace(to_replace="\s:00", value="")]

输出应为:

ID, Date
1 8/24/1995
2 8/1/1899


推荐答案

您需要将修剪后的列分配回原始列,而不是进行子集设置,并且 str.replace 方法似乎没有 to_replace value 参数。而是使用 pat repl 参数:

You need to assign the trimmed column back to the original column instead of doing subsetting, and also the str.replace method doesn't seem to have the to_replace and value parameter. It has pat and repl parameter instead:

df["Date"] = df["Date"].str.replace("\s:00", "")

df
#   ID       Date
#0   1  8/24/1995
#1   2   8/1/1899

这篇关于如何在数据框列中删除字符串的子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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