misc#
text.name_replace#
- neurotools.misc.text.name_replace(in_obj, to_replace, replace_with)#
Helper function to perform simple text replacement but over arbitrary objects.
- Parameters
in_obj (list, set or
pandas.DataFrame
) –The object in which to replace all sub objects of, or in the case of DataFrame input the column names of.
Note: This replacement occurs in some cases in-place rather than on a copy of the object (except in the case of set input).
to_replace (str or list of str) – The str in which to replace with other argument replace_with. In the case that a list of str is passed, then any of the elements of the passed list will be if found replaced with the passed argument for replace_with.
replace_with (str) – The str in which all instances of to_replace should be replaced with.
- Returns
out_obj – The in_obj but with text replacement performed / column name replacement.
- Return type
list, set or
pandas.DataFrame
Examples
In [1]: import pandas as pd In [2]: from neurotools.misc.text import name_replace # Base case In [3]: name_replace(['cow123', 'goat123', 'somethingelse'], '123', '456') Out[3]: ['cow456', 'goat456', 'somethingelse'] # DataFrame case In [4]: df = pd.DataFrame(columns=['cow123', 'goat123', 'somethingelse']) In [5]: name_replace(df, '123', '456') Out[5]: Empty DataFrame Columns: [cow456, goat456, somethingelse] Index: [] # to_replace list case In [6]: name_replace(in_obj=['cow123', 'goat123', 'somethingelse'], ...: to_replace=['123', 'something'], ...: replace_with='456') ...: Out[6]: ['cow456', 'goat456', '456else']