简体   繁体   中英

Replace partial string in Pandas Dataframe with dictionary value

I have a Pandas DataFrame (df) where some of the words contain encoding replacement characters. I want to replace these words with replacement words from a dictionary (translations).

translations = {'gr�nn': 'gronn', 'm�nst': 'menst'}
df = pd.DataFrame(["gr�nn Y", "One gr�nn", "Y m�nst/line X"])

df.replace(translations, regex=True, inplace=True)

However, it doesn't seem to capture all the instances. Current output:

                0
0         gronn Y
1       One gr�nn
2  Y m�nst/line X

Do I need to specify any regex patterns to enable the replacement to also capture partial words within a string?

Expected output:

                0
0         gronn Y
1       One gronn
2  Y menst/line X

Turn your translations into regex find/replace strings:

translations = {r'(.*)gr�nn(.*)': r'\1gronn\2', r'(.*)m�nst(.*)': r'\1menst\2'}
df = pd.DataFrame(["gr�nn Y", "One gr�nn", "Y m�nst/line X"])
df.replace(translations, regex=True)

Returns:

    0
0   gronn Y
1   One gronn
2   Y menst/line X

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM