pandas: dropna with how, subset and thresh

dropna has options that change its answer completely. Model them, including the combination pandas refuses.

What to build

A program that answers dropna queries on a table.

Requirements

  • Each query is a list of options: how any, how all, thresh N, subset A,B; the default is how any over all columns. Print kept: I I ... with the positions of the rows that stay, or kept: none.
  • how and thresh together are refused, as pandas refuses them: print TypeError: You cannot set both the how and thresh arguments at the same time.

Input

Line 1: the header. Line 2: n. Then n rows; an empty value is missing. Next line: q. Then q queries (an empty line means no options).

Output

q lines.

Constraints

1 ≤ n ≤ 100; 1 ≤ q ≤ 20; subset names exist.

Example

Input:

a,b,c
4
1,2,3
1,,3
,,
,,9
4

how all
subset a,b
thresh 2

Output:

kept: 0
kept: 0 1 3
kept: 0
kept: 0 1

Why it works

Every option is a rule about how many values a row has among the chosen columns: any gap, no values, or fewer than N; thresh is itself a different "how", which is why pandas will not take both.

Examples

Example 1 · Input
a,b,c
4
1,2,3
1,,3
,,
,,9
4

how all
subset a,b
thresh 2
Expected output
kept: 0
kept: 0 1 3
kept: 0
kept: 0 1
Example 2 · Input
x,y
2
1,
,2
2
how any subset y
how all thresh 1
Expected output
kept: 1
TypeError: You cannot set both the how and thresh arguments at the same time.

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

Hints

Hint 1

For each row, count the present values among the columns that matter (all of them, or the subset).

Hint 2

how=any drops a row with any gap, how=all only a row with no values at all, and thresh keeps rows with at least that many values present.

Solution

Show one correct solution (try it yourself first)
header = input().split(",")
n = int(input())
rows = [input().split(",") for _ in range(n)]
q = int(input())
for _ in range(q):
    words = input().split()
    options = dict(zip(words[::2], words[1::2]))
    if "how" in options and "thresh" in options:
        print("TypeError: You cannot set both the how and thresh arguments at the same time.")
        continue
    columns = options["subset"].split(",") if "subset" in options else header
    at = [header.index(c) for c in columns]
    kept = []
    for i, row in enumerate(rows):
        present = sum(1 for j in at if row[j] != "")
        if "thresh" in options:
            keep = present >= int(options["thresh"])
        elif options.get("how", "any") == "all":
            keep = present > 0
        else:
            keep = present == len(at)
        if keep:
            kept.append(str(i))
    print("kept: " + (" ".join(kept) if kept else "none"))

Lesson: Missing data: finding, dropping and filling →