How to replace text in a string column of a Pandas dataframe?

Use the vectorised str method replace: df[‘range’] = df[‘range’].str.replace(‘,’,’-‘) df range 0 (2-30) 1 (50-290) EDIT: so if we look at what you tried and why it didn’t work: df[‘range’].replace(‘,’,’-‘,inplace=True) from the docs we see this description: str or regex: str: string exactly matching to_replace will be replaced with value So because the str values … Read more

How do I perform a Perl substitution on a string while keeping the original?

This is the idiom I’ve always used to get a modified copy of a string without changing the original: (my $newstring = $oldstring) =~ s/foo/bar/g; In perl 5.14.0 or later, you can use the new /r non-destructive substitution modifier: my $newstring = $oldstring =~ s/foo/bar/gr; NOTE: The above solutions work without g too. They also … Read more

Find and replace – Add carriage return OR Newline

Make sure Use: Regular expressions is selected in the Find and Replace dialog: Note that for Visual Studio 2010, this doesn’t work in the Visual Studio Productivity Power Tools’ Quick Find extension (as of the July 2011 update); instead, you’ll need to use the full Find and Replace dialog (use Ctrl+Shift+H, or Edit –> Find … Read more

Removing a list of characters in string

If you’re using python2 and your inputs are strings (not unicodes), the absolutely best method is str.translate: >>> chars_to_remove = [‘.’, ‘!’, ‘?’] >>> subj = ‘A.B!C?’ >>> subj.translate(None, ”.join(chars_to_remove)) ‘ABC’ Otherwise, there are following options to consider: A. Iterate the subject char by char, omit unwanted characters and join the resulting list: >>> sc … Read more

Replace only some groups with Regex

A good idea could be to encapsulate everything inside groups, no matter if need to identify them or not. That way you can use them in your replacement string. For example: var pattern = @”(-)(\d+)(-)”; var replaced = Regex.Replace(text, pattern, “$1AA$3”); or using a MatchEvaluator: var replaced = Regex.Replace(text, pattern, m => m.Groups[1].Value + “AA” … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)