python/pandas:使用正则表达式删除列中以指定内容开头的数据

2024-10-04 11:24:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种有效的方法来删除数据帧列中字符串中不需要的部分。你知道吗

例如,我要删除的字符串是0x0469,还有许多其他字符串都以“0x”开头。如何删除所有以“0x”开头的字符串并将其替换为0

部分数据帧示例

  Index Totbytes Dur
0       500      30.11344
1       262      0x00469
2       345      45.33333

Tags: 数据方法字符串示例indexdurtotbytes
1条回答
网友
1楼 · 发布于 2024-10-04 11:24:36

既然你要求regex

s = df['Dur'].str.match('^(0x).*')

其中^表示字符串的开头,表示:

0    False
1     True
2    False
Name: Dur, dtype: bool

所以:

df[~s]

给你:

   Index  Totbytes       Dur
0      0       500  30.11344
2      2       345  45.33333

相关问题 更多 >