python-3.x - 如何在 Python 中从 pandas DataFrame 中删除字符串的一部分
问题描述
我有这个数据框:
df=pd.DataFrame({'a': [2, 6, 8, 9],
'date': ['2021-07-21 04:34:02',
'test_2022-17-21 04:54:22',
'test_2020-06-21 04:34:02',
'2023-12-01 11:54:52']})
df["date"].replace("test_", "")
df
我想从日期列中删除“test_”。也许,你可以帮忙
解决方案
用于str.strip(<unnecessary string>)
删除不必要的字符串:
df.date = df.date.str.strip('test_')
输出:
a date
0 2 2021-07-21 04:34:02
1 6 2022-17-21 04:54:22
2 8 2020-06-21 04:34:02
3 9 2023-12-01 11:54:52
推荐阅读
- c# - ReSharper C# Intellisense 在键入 Override 关键字后显示可覆盖的成员
- c# - 如何生成包含 JSON 结构的文件以供下载?
- java - 在 GlassFish 上创建和运行 RESTful Web 服务
- python - 查找位于其他两个 DataFrame 的索引值之间的 DataFrame 的索引值
- java - checkstyle非法进口的建议包
- php - 在 array_shift 之后无法向数组添加值
- c# - 如何使用带有 C# 的 Linq 尽可能简单地替换 List 中的此项?
- spring-integration - 序列化后的消息有效负载大于应有的大小
- reactjs - TypeError:无法读取未定义的属性“isRequired”
- msbuild - 在 Azure Devops 上构建和发布失败