how to replace characters in a dataframe where column may have different data types entries

Viewed 74

new to python want to ask a quick question on how to replace multiple characters simultaneously given that the entries may have different data types. I just want to change the strings and keep everything else as it is:

import pandas as pd

def test_me(text):
    replacements = [("ID", ""),("u", "a")] # 
    return [text.replace(a, b) for a, b in replacements if type(text) == str]

cars = {'Brand': ['HonduIDCivic', 1, 3.2,'CarIDA4'],
        'Price': [22000,25000,27000,35000]
        }

df = pd.DataFrame(cars, columns = ['Brand', 'Price'])
df['Brand'] = df['Brand'].apply(test_me)

resulting in

    Brand                       Price
0   [HonduCivic, HondaIDCivic]  22000
1   []                          25000
2   []                          27000
3   [CarA4, CarIDA4]            35000

rather than

    Brand                       Price
0   HondaCivic                  22000
1   1                           25000
2   3.2                         27000
3   CarA4                       35000

Appreciate any suggestions!

1 Answers

If the replacements never have identical search phrases, it will be easier to convert the list of tuples into a dictionary and then use

import re
#...
def test_me(text):
    replacements = dict([("ID", ""),("u", "a")])
    if type(text) == str:
        return re.sub("|".join(sorted(map(re.escape, replacements.keys()),key=len,reverse=True)), lambda x: replacements[x.group()], text)
    else:
        return text

The "|".join(sorted(map(re.escape, replacements.keys()),key=len,reverse=True)) part will create a regular expression out of re.escaped dictionary keys starting with the longest so as to avoid issues when handling nested search phrases that share the same prefix.

Pandas test:

>>> df['Brand'].apply(test_me)
0    HondaCivic
1             1
2           3.2
3         CarA4
Name: Brand, dtype: object
Related