Skip to content

Instantly share code, notes, and snippets.

@data-enhanced
Last active February 12, 2021 14:51
Show Gist options
  • Save data-enhanced/0c48586f36e8cd27ffad8fe076d62c4e to your computer and use it in GitHub Desktop.
Save data-enhanced/0c48586f36e8cd27ffad8fe076d62c4e to your computer and use it in GitHub Desktop.

Revisions

  1. David Cochran revised this gist Feb 12, 2021. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions pandas_describe_formatted.md
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,17 @@
    # Pandas Describe() formatted
    # Pandas .describe() formatted
    Format numbers output from the pandas df.describe() method. For instance, instead of outputting scientific notation, we can have numbers with thousands separators and a desired number of decimals.

    For reference, see:
    - https://stackoverflow.com/a/47207283
    - https://mkaz.blog/code/python-string-format-cookbook/


    When using describe with an entire dataframe, use .apply and a lambda function to apply the formatting to every number.
    When using .describe with an entire dataframe, use .apply and a lambda function to apply the formatting to every number.
    - To change the number of decimals, change the number before the f
    - To remove the thousands separator remove the comma

    `df.describe().apply(lambda s: s.apply('{:,.0f}'.format))`

    When using describe with a single column or a series, use the .map method instead:
    When using .describe with a single column or a series, use the .map method instead:

    `df['Column'].describe().map('{:,.0f}'.format)`
    `df['Column'].describe().map('{:,.0f}'.format)`

    For reference, see:
    - https://stackoverflow.com/a/47207283
    - https://mkaz.blog/code/python-string-format-cookbook/
    - https://docs.python.org/3/library/string.html#formatstrings
  2. David Cochran revised this gist Feb 12, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pandas_describe_formatted.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ For reference, see:
    - https://mkaz.blog/code/python-string-format-cookbook/


    When using describe for a data frame, use .apply and a lambda function to apply the formatting to every number.
    When using describe with an entire dataframe, use .apply and a lambda function to apply the formatting to every number.
    - To change the number of decimals, change the number before the f
    - To remove the thousands separator remove the comma

  3. David Cochran revised this gist Feb 12, 2021. 1 changed file with 12 additions and 11 deletions.
    23 changes: 12 additions & 11 deletions pandas_describe_formatted.md
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,17 @@
    # Pandas Describe() formatted
    Format numbers output from the pandas df.describe() method.
    Format numbers output from the pandas df.describe() method. For instance, instead of outputting scientific notation, we can have numbers with thousands separators and a desired number of decimals.

    For instance, instead of outputting scientific notation, we can have numbers with thousands separators and a desired number of decimals.
    For reference, see:
    - https://stackoverflow.com/a/47207283
    - https://mkaz.blog/code/python-string-format-cookbook/

    When using describe for a data frame,
    # Uses a lambda function to apply the formatting to every number
    # See https://stackoverflow.com/a/47207283
    # See also https://mkaz.blog/code/python-string-format-cookbook/
    # To change the number of decimals, change the number before the f
    # To remove the thousands separator remove the comma

    df.describe().apply(lambda s: s.apply('{:,.0f}'.format))
    When using describe for a data frame, use .apply and a lambda function to apply the formatting to every number.
    - To change the number of decimals, change the number before the f
    - To remove the thousands separator remove the comma

    # Use .map for a series
    df['Column'].describe().map('{:,.0f}'.format)
    `df.describe().apply(lambda s: s.apply('{:,.0f}'.format))`

    When using describe with a single column or a series, use the .map method instead:

    `df['Column'].describe().map('{:,.0f}'.format)`
  4. David Cochran renamed this gist Feb 12, 2021. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions pandas_describe_formatted.py → pandas_describe_formatted.md
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,9 @@
    # Describe() formatted
    # Format numbers without scientific notation and with thousands separators
    # Pandas Describe() formatted
    Format numbers output from the pandas df.describe() method.

    For instance, instead of outputting scientific notation, we can have numbers with thousands separators and a desired number of decimals.

    When using describe for a data frame,
    # Uses a lambda function to apply the formatting to every number
    # See https://stackoverflow.com/a/47207283
    # See also https://mkaz.blog/code/python-string-format-cookbook/
  5. David Cochran revised this gist Feb 12, 2021. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion pandas_describe_formatted.py
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,7 @@
    # To change the number of decimals, change the number before the f
    # To remove the thousands separator remove the comma

    df.describe().apply(lambda s: s.apply('{:,.0f}'.format))
    df.describe().apply(lambda s: s.apply('{:,.0f}'.format))

    # Use .map for a series
    df['Column'].describe().map('{:,.0f}'.format)
  6. David Cochran revised this gist Feb 12, 2021. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions pandas_describe_formatted.py
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,9 @@
    # Describe() formatted
    # Format numbers without scientific notation and with thousands separators
    # To change the number of decimals, change the number before the f
    # To remove the thousands separator remove the comma
    # Uses a lambda function to apply the formatting to every number
    # See https://stackoverflow.com/a/47207283
    # See also https://mkaz.blog/code/python-string-format-cookbook/
    # To change the number of decimals, change the number before the f
    # To remove the thousands separator remove the comma

    df.describe().apply(lambda s: s.apply('{:,.0f}'.format))
  7. David Cochran revised this gist Feb 12, 2021. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions pandas_describe_formatted.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    # Describe() formatted
    # Format numbers without scientific notation and with thousands separators
    # To change the number of decimals, change the number before the f
    # To remove the thousands separator remove the comma
    # See https://stackoverflow.com/a/47207283
    # See also https://mkaz.blog/code/python-string-format-cookbook/

  8. David Cochran revised this gist Feb 12, 2021. 1 changed file with 6 additions and 5 deletions.
    11 changes: 6 additions & 5 deletions pandas_describe_formatted.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    # df.describe() with numbers converted to normal format with thousands separators
    # https://stackoverflow.com/a/47207283
    # https://mkaz.blog/code/python-string-format-cookbook/
    # To change the number of decimals, change the 0 to the desired number
    df.describe().apply(lambda s: s.apply('{:,.0f}'.format))
    # Describe() formatted
    # Format numbers without scientific notation and with thousands separators
    # To change the number of decimals, change the number before the f
    # See https://stackoverflow.com/a/47207283
    # See also https://mkaz.blog/code/python-string-format-cookbook/

    df.describe().apply(lambda s: s.apply('{:,.0f}'.format))
  9. David Cochran revised this gist Feb 12, 2021. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion pandas_describe_formatted.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    # df.describe() with numbers converted to normal format with thousands separators
    # https://stackoverflow.com/a/47207283
    # https://mkaz.blog/code/python-string-format-cookbook/
    df.describe().apply(lambda s: s.apply('{:,.0f}'.format))
    # To change the number of decimals, change the 0 to the desired number
    df.describe().apply(lambda s: s.apply('{:,.0f}'.format))

  10. David Cochran created this gist Feb 12, 2021.
    4 changes: 4 additions & 0 deletions pandas_describe_formatted.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # df.describe() with numbers converted to normal format with thousands separators
    # https://stackoverflow.com/a/47207283
    # https://mkaz.blog/code/python-string-format-cookbook/
    df.describe().apply(lambda s: s.apply('{:,.0f}'.format))