DEV Community

StagingBench Team
StagingBench Team

Posted on AI-assisted

Count Virtual Staging Features with Python's Counter

A virtual staging comparison table has two kinds of information to preserve when you summarize it. One is the answer recorded for a feature. The other is whether the table has an answer at all. In Python, use True for a recorded offer, False for a recorded absence, and None for a blank. collections.Counter can count each of those values directly. The table below comes from the public StagingBench data.

The embedded dataset follows the source order. The features tuple contains the column names, and each row starts with the tool identifier, followed by the feature values in that order. There are 13 rows and 10 feature columns. The identifiers let you trace an entry back to its row. The count uses only the feature values.

Start by taking row[1:] from each row. The slice leaves the identifier out and preserves every feature cell, including trailing blanks represented by None. The inner zip takes the first value from each sliced row to form the empty room staging column, then takes the next value from each row to form the decluttering column, and continues in the same order. The outer zip pairs each resulting column with its name.

Construct a Counter from each column. Reading counts[True], counts[False], and counts[None] gives the three figures to print. A Counter returns zero for a missing key, so a column with no recorded absences prints a zero in the False position. Avoid counting by truthiness here. Both False and None are falsey, and grouping them by that property would erase the distinction between an explicit absence and a missing answer.

Call overall.update(counts) after each column to add its frequencies to the overall counter. Passing the column counter to update adds the counts associated with its keys. By the time the loop finishes, the overall counter covers every feature cell. Summing its values gives the total, including unknowns. The width calculation uses the longest feature name to align the numeric columns in plain text.

Read the output one column at a time. Day to dusk has six recorded offers, zero recorded absences, and seven unknowns. Six is the lower bound supported by those records. Furnished restyling also has six recorded offers, alongside one recorded absence and six unknowns. Showing all three counts preserves a difference the offered count alone would hide.

To check the aggregation, read across the overall row. It has 68 offers, two recorded absences, and 60 unknowns. Together they account for all 130 feature cells. The printed total uses the same three states as the per feature rows.

from collections import Counter

features = (
    "empty_room_staging", "declutter", "furnished_restyle", "day_to_dusk",
    "sky_replacement", "renovation_recolor", "tour_360_video", "api",
    "mls_disclaimer", "commercial_license",
)

rows = (
    ("T01", True, None, True, None, True, None, None, None, None, None),
    ("T02", True, True, True, True, True, True, True, True, True, True),
    ("T03", True, True, None, None, None, None, True, None, True, True),
    ("T04", True, True, None, None, True, True, True, True, None, None),
    ("T05", True, True, True, True, True, True, None, True, None, None),
    ("T06", True, False, False, None, None, None, None, None, None, None),
    ("T07", True, None, None, None, None, None, True, True, None, None),
    ("T08", True, True, None, True, None, None, None, None, True, None),
    ("T09", True, True, None, True, None, True, None, True, None, None),
    ("T10", True, None, None, True, True, True, True, None, None, None),
    ("T11", True, True, True, None, True, True, True, True, None, True),
    ("T12", True, True, True, True, True, None, None, None, True, True),
    ("T13", True, True, True, None, None, None, True, None, None, None),
)

width = max(map(len, features))
overall = Counter()
print(f'{"Feature":<{width}}  {"True":>4}  {"False":>5}  {"None":>4}')
for feature, column in zip(features, zip(*(row[1:] for row in rows))):
    counts = Counter(column)
    overall.update(counts)
    print(f"{feature:<{width}}  {counts[True]:>4}  {counts[False]:>5}  {counts[None]:>4}")

print(f'{"Overall":<{width}}  {overall[True]:>4}  {overall[False]:>5}  {overall[None]:>4}')
print(f"Feature cells = {sum(overall.values())}")
Enter fullscreen mode Exit fullscreen mode

Output:

Feature             True  False  None
empty_room_staging    13      0     0
declutter              9      1     3
furnished_restyle      6      1     6
day_to_dusk            6      0     7
sky_replacement        7      0     6
renovation_recolor     6      0     7
tour_360_video         7      0     6
api                    6      0     7
mls_disclaimer         4      0     9
commercial_license     4      0     9
Overall               68      2    60
Feature cells = 130
Enter fullscreen mode Exit fullscreen mode

Top comments (0)