pandas: transform and filter

Model transform, which gives every row its group's result, and filter, which keeps or drops whole groups.

What to build

A program that compares each row with its group and keeps the larger groups.

Requirements

  • For each row in input order print NAME GROUP VALUE share=P% diff=D, where P is the value's share of its group's total (one decimal) and D the value minus the group's mean (two decimals, with a sign: +1.50, -0.25, +0.00).
  • Then print kept groups: G1, G2, ..., the groups with at least K rows in sorted order (or none), and kept rows: R.

Input

Line 1: K. Line 2: n. Then n lines NAME GROUP VALUE.

Output

n lines, then two lines.

Constraints

1 ≤ n ≤ 200; values are positive whole numbers, so every group total is positive.

Example

Input:

2
5
o1 Dhaka 30
o2 Paris 50
o3 Dhaka 20
o4 Lima 10
o5 Paris 25

Output:

o1 Dhaka 30 share=60.0% diff=+5.00
o2 Paris 50 share=66.7% diff=+12.50
o3 Dhaka 20 share=40.0% diff=-5.00
o4 Lima 10 share=100.0% diff=+0.00
o5 Paris 25 share=33.3% diff=-12.50
kept groups: Dhaka, Paris
kept rows: 4

Why it works

transform computes per group but returns per row, which is what makes "share of group" and "distance from group mean" one line each in pandas, while filter judges each group as a whole and keeps all of its rows or none.

Examples

Example 1 · Input
2
5
o1 Dhaka 30
o2 Paris 50
o3 Dhaka 20
o4 Lima 10
o5 Paris 25
Expected output
o1 Dhaka 30 share=60.0% diff=+5.00
o2 Paris 50 share=66.7% diff=+12.50
o3 Dhaka 20 share=40.0% diff=-5.00
o4 Lima 10 share=100.0% diff=+0.00
o5 Paris 25 share=33.3% diff=-12.50
kept groups: Dhaka, Paris
kept rows: 4
Example 2 · Input
3
2
a x 1
b y 3
Expected output
a x 1 share=100.0% diff=+0.00
b y 3 share=100.0% diff=+0.00
kept groups: none
kept rows: 0

Prompts inside input() are ignored when checking, so input("n: ") is fine.

Hints

Hint 1

First compute each group's total and mean, then go back through the rows and look up their own group's figures.

Hint 2

filter keeps a group when its number of rows is at least K.

Solution

Show one correct solution (try it yourself first)
k = int(input())
n = int(input())
rows = [input().split() for _ in range(n)]
groups = {}
for name, group, value in rows:
    groups.setdefault(group, []).append(int(value))
total = {g: sum(v) for g, v in groups.items()}
mean = {g: sum(v) / len(v) for g, v in groups.items()}
for name, group, value in rows:
    v = int(value)
    print(f"{name} {group} {value} share={100 * v / total[group]:.1f}% diff={v - mean[group]:+.2f}")
kept = sorted(g for g, v in groups.items() if len(v) >= k)
print("kept groups: " + (", ".join(kept) if kept else "none"))
print(f"kept rows: {sum(len(groups[g]) for g in kept)}")

Lesson: groupby: split, apply, combine →