Elizabeth S
Elizabeth S

Reputation: 316

Remove * from a specific column value

For this dataframe, what is the best way to get ride of the * of "Stad Brussel*". In the real dataframe, the * is also on the upside. Please refer to the pic. Thanks.

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

Desired results:

   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

Upvotes: 0

Views: 55

Answers (3)

Ali Ülkü
Ali Ülkü

Reputation: 106

for dataframe, first define column(s) to be checked:

cols_to_check = ['4']

then,

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

Upvotes: 0

Stefan
Stefan

Reputation: 957

You can try:

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

This will remove all * characters in the 'Dutch name' column. If you need to remove the character from multiple columns use:

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

Upvotes: 1

Welgriv
Welgriv

Reputation: 823

If you manipulate only strings you can use regular expression matching. See here.

Something like :

import re

txt = 'Your file as a string here'

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

out now contain what you want.

Upvotes: 0

Related Questions