piper.verbs.replace_names

piper.verbs.replace_names(df: pandas.core.frame.DataFrame, dict_: Dict, info: bool = False)pandas.core.frame.DataFrame[source]

replace column names (or partially) with dictionary values

Examples

dict_ = { 'number$': 'nbr', 'revenue per cookie': 'unit revenue', 'cost per cookie': 'unit cost',
      'month': 'mth', 'revenue per cookie': 'unit revenue', 'product': 'item', 'year': 'yr'}

cols = ['Country', 'Product', 'Units Sold', 'Revenue per cookie', 'Cost per cookie',
        'Revenue', 'Cost', 'Profit', 'Date', 'Month Number', 'Month Name', 'Year']

expected = ['country','item', 'units_sold', 'unit_revenue', 'unit_cost', 'revenue', 'cost',
            'profit', 'date', 'mth_nbr', 'mth_name', 'yr']

df = pd.DataFrame(None, columns=cols)
df = replace_names(df, dict_, info=False)
df = clean_names(df)

assert expected == list(df.columns)
Parameters
  • df – a pandas dataframe

  • values – dictionary of from/to values (optionally) with regex values

  • info – Default False. If True, print replacement from/to values.

Returns

Return type

a pandas dataframe