Number

We wish to format the string representation of numbers in a certain way typically for display purposes.

We will cover two common scenarios which are:

  • Scientific Notation: Specify whether scientific notation should be used in the string representation of a number.
  • Comma Separator: Add commas to separate each three digits in the string representation of large numbers.
  • Decimal Places: Specify the number of decimal places to have in the string representation of a floating point number.

This section is concerned with the string representation of numbers. See Numeric for coverage of the display settings of numbers.

Scientific Notation

df_2 = df.assign(
    col_2 = df['col_1'].map('{:.0f}'.format)
)

Here is how this works:

  • Scientific notation is representing 1000 as 1e+03 and is often triggered automatically when displaying large numbers.
  • To obtain a string representation of a number without scientific notation, we can use the function format() applied to the string (format specifier) '{:.0f}'. See Decimal Places below for a description of this format specifier.
  • We use the function map() to iterate over each value of the column col_1 and apply the function format() as described above. Equivalently, we can use apply() with axis=1.

Comma Separator

We wish to add comma separators between each three digits in the string representation of large numbers.

In this example, we wish to create a new column col_2 that is a string representation of the numeric column col_1 with commas added between every three digits.

df_2 = df.assign(
    col_2 = df['col_1'].map('{:,d}'.format)
)

Here is how this works:

  • It is common to add commas after every three digits of large numbers to enhance readability e.g. 1000000 is displayed as 1,000,000.
  • To obtain a string representation of a number where every three digits are separated by a comma, we can use the function format() applied to the string (format specifier) '{:,d}' .
    • The ':' inside the placeholder is used to specify the formatting options for the value that will be passed to the placeholder.
    • The ',' inside the placeholder is used to add a comma as a thousands separator.
    • The 'd' inside the placeholder is used to format the value as a decimal number.
  • We use the function map() to iterate over each value of the column col_1 and apply the function format() as described above. Equivalently, we can use apply() with axis=1.

Decimal Places

We wish to specify the number of decimal places (after the decimal point) to display in a string representation of a floating point number.

df_2 = df.assign(
    col_2 = df['col_1'].round(2).map('{:.2f}'.format)
)

Here is how this works:

  • In df['col_1'].round(2), we use the method round() to round each number in the column col_1 to two decimal places.
  • To obtain a string representation of a number with a minimum of two decimal places, we can use the function format() applied to the string (format specifier) '{:,d}'.
    • The ':' inside the placeholder is used to specify the formatting options for the value that will be passed to the placeholder.
    • The '.' inside the placeholder is used to specify the number of decimal places.
    • The '2' after the dot is the desired number of decimal places.
    • The 'f' inside the placeholder is used to format the value as a floating-point number.
  • We use the function map() to iterate over each value of the column col_1 and apply the function format() as described above. Equivalently, we can use apply() with axis=1.

Extension: String to Numeric to String

df_2 = df.assign(
    col_2 = df['col_1']
    .astype(float)
    .round(2)
    .map('{:.2f}'.format)
)

Here is how this works:

  • In case the input col_1 is a string, we will first need to convert it to a numeric data type so we may round it.
  • We use astype(float) to convert a string to a numeric data type. See Numeric.
  • We then apply round and format as covered in the primary solution above.
PYTHON
I/O