Ad

How To Distinguish Unique Value In A Dataframe Column And Count Its Other Columns?

- 1 answer

I have a dataframe exactly looks like this. As you can see, I have performed 10 iteration steps to obtain following result.

enter image description here

In a row, column B describes a specific person, while column D and E determine the location in terms of (x y points) as they standing in frame n.

My question is, how am I going to perform counting in each iteration to determine whether any person is standing around the area of interest overtime? If they are standing in the region of interest, I could increase the counter by 1.

region_x = np.min(monitor_region[:,0])
region_y = np.min(monitor_region[:,1])
region_width = monitor_region.shape[1]
region_height = monitor_region.shape[0]
counter = 0

#Looking for logic here to distinguish specific person based on column B value
    if((region_x < person["point_x"] < (region_x + region_width)) and (region_y < person["point_y"] < (region_y + region_height))):
        print(counter += 1)

The desired outcome is that, I can use the "if" statement to increase the counter of each individual standing within region of interest.

E.g. Person 1 stands 3 times in region of interest. Person 2 stands 10 times in region of interest.

Ad

Answer

Store the person and the count in a dictionary

NB: I think this requires python >=3.6 but only for the f-string print of the dictionary at the end.

from collections import defaultdict
from pprint import pprint
from random import randint

import numpy as np


def fetch_data():
    return {
        "Person": randint(1, 10),
        "Frame": randint(1, 10),
        "X": randint(0, 500),
        "Y": randint(1, 10),
    }


people_in_roi = defaultdict(lambda: 0)

for i in range(100):

    data = fetch_data()

    if data["X"] > 250:
        people_in_roi[data["Person"]] += 1

for k, v in people_in_roi.items():
    print(f"Person: {k}\tIn ROI count: {v}")

OUTPUT:

Person: 4       In ROI count: 3
Person: 2       In ROI count: 7
Person: 9       In ROI count: 8
Person: 3       In ROI count: 2
Person: 1       In ROI count: 3
Person: 10      In ROI count: 4
Person: 5       In ROI count: 5
Person: 8       In ROI count: 2
Person: 7       In ROI count: 3
Person: 6       In ROI count: 4
Ad
source: stackoverflow.com
Ad