I have a large dataset (~5 Mio rows) with results from a Machine Learning training. Now I want to check to see if the results hit the "target range" or not. Lets say this range contains all values between -0.25 and +0.25. If it's inside this range, it's a Hit, if it's below Low and on the other side High.
I now would create this three columns Hit, Low, High and calculate for each row which condition applies and put a 1 into this col, the other two would become 0. After that I would group the values and sum them up. But I suspect there must be a better and faster way, such as calculate it directly while grouping.
Data
import pandas as pd
df = pd.DataFrame({"Type":["RF", "RF", "RF", "MLP", "MLP", "MLP"], "Value":[-1.5,-0.1,1.7,0.2,-0.7,-0.6]})
+----+--------+---------+
| | Type | Value |
|----+--------+---------|
| 0 | RF | -1.5 | <- Low
| 1 | RF | -0.1 | <- Hit
| 2 | RF | 1.7 | <- High
| 3 | MLP | 0.2 | <- Hit
| 4 | MLP | -0.7 | <- Low
| 5 | MLP | -0.6 | <- Low
+----+--------+---------+
Expected Output
pd.DataFrame({"Type":["RF", "MLP"], "Low":[1,2], "Hit":[1,1], "High":[1,0]})
+----+--------+-------+-------+--------+
| | Type | Low | Hit | High |
|----+--------+-------+-------+--------|
| 0 | RF | 1 | 1 | 1 |
| 1 | MLP | 2 | 1 | 0 |
+----+--------+-------+-------+--------+