Skip to content

Series.replace() to apply() + re.sub() #7

@baziotis

Description

@baziotis

Original

df['Name'].replace(to_replace='John', value='Mike', regex=True, inplace=True)

Rewritten

df['Name'] = df['Name'].apply(lambda x: re.sub(pattern='John', repl='Mike', string=x))

This is another weird case where the Pandas-provided method is slower.
Full example:

import pandas as pd
import re
df = pd.read_csv('https://web.stanford.edu/class/archive/cs/cs109/cs109.1166/stuff/titanic.csv')
df = pd.concat([df]*2000, ignore_index=True)
df['Name'].replace(to_replace='John', value='Mike', regex=True, inplace=True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    patternA pattern that Dias does not already rewrite into a faster version.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions