Replace invalid values with None in Pandas DataFrame

Actually in later versions of pandas this will give a TypeError: df.replace(‘-‘, None) TypeError: If “to_replace” and “value” are both None then regex must be a mapping You can do it by passing either a list or a dictionary: In [11]: df.replace(‘-‘, df.replace([‘-‘], [None]) # or .replace(‘-‘, {0: None}) Out[11]: 0 0 None 1 3 … Read more

How do I replace the *first instance* of a string in .NET?

string ReplaceFirst(string text, string search, string replace) { int pos = text.IndexOf(search); if (pos < 0) { return text; } return text.Substring(0, pos) + replace + text.Substring(pos + search.Length); } Example: string str = “The brown brown fox jumps over the lazy dog”; str = ReplaceFirst(str, “brown”, “quick”); EDIT: As @itsmatt mentioned, there’s also Regex.Replace(String, … Read more

Replace word with contents of paste buffer?

I’m thinking by “paste” you mean the unnamed (yank/put/change/delete/substitute) register, right? (Since that’s the one that’d get overwritten by the change command.) Registers are generally specified by typing ” then the name (single character) of the register, like “ay then “ap to yank into register a, then put the contents of register a. Same goes … Read more

Can I replace groups in Java regex?

Use $n (where n is a digit) to refer to captured subsequences in replaceFirst(…). I’m assuming you wanted to replace the first group with the literal string “number” and the second group with the value of the first group. Pattern p = Pattern.compile(“(\\d)(.*)(\\d)”); String input = “6 example input 4”; Matcher m = p.matcher(input); if … Read more