I have a dataframe which consists of the five columns "ID", "Name", "pos_x", "pos_y" and "Volume", something like this:
ID | Name | pos_x | pos_y | volume
1 | A | 1 | 1.5 | 10
2 | A | 3.5 | 3 | 6
3 | A | 4 | 4 | 8
4 | A | 4.5 | 4.5 | 9
5 | A | 5 | 6 | 10
1 | B | 1.2 | 1.2 | 4
3 | B | 4.3 | 4.4 | 8
4 | B | 4.5 | 4.2 | 7
2 | C | 3 | 3.3 | 9
3 | C | 4.2 | 4.1 | 10
I would now like to create a new column in the dataframe ("volume_avg") in which the average of the volume of all ID`s is calculated, which lie within a radius of 2.5 mm to the respective ID.
My idea was to first run through each ID for each name (because the name + ID is the primary key) and then look at the position x and y for each ID. With an if loop I would then check if the ID is a neighbor or not.
So the conditions would be:
if pos_x (current ID) < pos_x (all other IDs) - 2.5 OR > pos_x (all other IDs) + 2.5
else if pos_y (current ID) < pos_y (all other IDs) - 2.5 OR > pos_y (all other IDs) + 2.5
then calculate sum(volume)/count(ID's)
otherwise nothing
Unfortunately I don't know how to write these considerations in a python code.... I would be glad if you can help me. Thanks a lot.
David