Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
baziotis opened this issue Apr 6, 2023 · 0 comments
Open

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

baziotis opened this issue Apr 6, 2023 · 0 comments
Labels
pattern A pattern that Dias does not already rewrite into a faster version.

Comments

@baziotis
Copy link
Collaborator

baziotis commented Apr 6, 2023

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)
@baziotis baziotis added the pattern A pattern that Dias does not already rewrite into a faster version. label Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pattern A pattern that Dias does not already rewrite into a faster version.
Projects
None yet
Development

No branches or pull requests

1 participant