从特定列值中删除*

2024-05-29 03:20:53 发布

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

对于这个数据帧,使用“斯塔德·布鲁塞尔*”的最佳方式是什么。在真实的数据帧中,*也处于上升趋势。请参考图片。谢谢

Dutch name             postcode Population
0   Anderlecht          1070    118241  
1   Oudergem            1160    33313   
2   Sint-Agatha-Berchem 1082    24701
3   Stad Brussel*       1000    176545
4   Etterbeek           1040    47414

预期结果:

   Dutch name              postcode Population
    0   Anderlecht          1070    118241  
    1   Oudergem            1160    33313   
    2   Sint-Agatha-Berchem 1082    24701
    3   Stad Brussel        1000    176545
    4   Etterbeek           1040    47414

enter image description here


Tags: 数据name方式趋势postcodepopulationdutchsint
3条回答

对于dataframe,首先定义要检查的列:

cols_to_check = ['4']

那么

df[cols_to_check] = df[cols_to_check].replace({'*':''}, regex=True)

如果只操作字符串,则可以使用正则表达式匹配。见here

比如:

import re

txt = 'Your file as a string here'

out = re.sub('\*', '', txt)

out现在包含您想要的内容

您可以尝试:

df['Dutch name'] = df['Dutch name'].replace({'\*':''}, regex = True)

这将删除“荷兰名称”列中的所有*字符。如果需要从多列中删除字符,请使用:

df.replace({'\*':''}, regex = True)

相关问题 更多 >

    热门问题