Skip to content

Commit

Permalink
Delimit big counts with commas
Browse files Browse the repository at this point in the history
  • Loading branch information
ccunningham101 committed Aug 2, 2023
1 parent a08d3ba commit 2525e10
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 18 deletions.
4 changes: 2 additions & 2 deletions analysis/report/report_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ def format_season_table(df, column_to_plot, category):
rr_cis_str = ci_to_str(rr_cis)
rr_cis_str.name = ("2023 v 2018", "Rate Ratio (95% CI)")

# Create table with count, rate (95% CI)
table["Count"] = table["Count"].applymap(lambda x: f"{x:.0f}")
# Create table with (comma delimited) count, rate (95% CI)
table["Count"] = table["Count"].applymap(lambda x: f"{x:,.0f}")
count_cis = (table[["Count", "Rate (95% CI)"]]).swaplevel(axis=1)
cols = count_cis.columns.get_level_values(0).unique()
reordered = count_cis.reindex(columns=cols, level="gas_year")
Expand Down
5 changes: 4 additions & 1 deletion analysis/report/table1.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ def series_to_bool(series):


def transform_percentage(x):
"""
Return (comma delimited) x and column percentage
"""
transformed = (
x.map("{:.0f}".format)
x.map("{:,.0f}".format)
+ " ("
+ (((x / x.sum()) * 100).round(1)).astype(str)
+ ")"
Expand Down
20 changes: 5 additions & 15 deletions analysis/report/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@
subset_table,
)
import pandas


def round_or_skip(x):
"""
Try to return a value
If it fails, return the original value
"""
try:
return round(float(x))
except Exception:
return x
import numpy


def count_table(measure_table):
Expand Down Expand Up @@ -96,10 +86,10 @@ def main():
table = count_table(subset)
table.index.name = table.index.name.title()
table.index = table.index.strftime("%d-%m-%y")
# NOTE: applymap is by value (so very slow), but we have a mix of strings
# and numbers (REDACTED) so pandas.to_numeric will not work
# and the tables are small
table = table.applymap(round_or_skip)
# NOTE: applymap is by value (so very slow), but the tables are small
table = table.applymap(
lambda x: f"{x:,.0f}" if ~numpy.isnan(x) else "[REDACTED]"
)
table.to_csv(output_dir / output_name)


Expand Down

0 comments on commit 2525e10

Please sign in to comment.