janitor.currency_column_to_numeric

janitor.currency_column_to_numeric(df: pandas.core.frame.DataFrame, column_name, cleaning_style: Optional[str] = None, cast_non_numeric: Optional[dict] = None, fill_all_non_numeric: Optional[Union[float, int]] = None, remove_non_numeric: bool = False) → pandas.core.frame.DataFrame[source]

Convert currency column to numeric.

This method does not mutate the original DataFrame.

This method allows one to take a column containing currency values, inadvertently imported as a string, and cast it as a float. This is usually the case when reading CSV files that were modified in Excel. Empty strings (i.e. ‘’) are retained as NaN values.

Parameters
  • df – The DataFrame

  • column_name – The column to modify

  • cleaning_style

    What style of cleaning to perform. If None, standard cleaning is applied. Options are:

    • ’accounting’:

    Replaces numbers in parentheses with negatives, removes commas.

  • cast_non_numeric – A dict of how to coerce certain strings. For example, if there are values of ‘REORDER’ in the DataFrame, {‘REORDER’: 0} will cast all instances of ‘REORDER’ to 0.

  • fill_all_non_numeric – Similar to cast_non_numeric, but fills all strings to the same value. For example, fill_all_non_numeric=1, will make everything that doesn’t coerce to a currency 1.

  • remove_non_numeric – Will remove rows of a DataFrame that contain non-numeric values in the column_name column. Defaults to False.

Returns

A pandas DataFrame.