# Simple rules based grouping algorithm in Python

**URL:** <https://forum.anaconda.com/t/simple-rules-based-grouping-algorithm-in-python/63785>\
**Category:** Data Science, ML & AI\
**Created:** [October 29, 2023, 5:20pm UTC](https://forum.anaconda.com/t/simple-rules-based-grouping-algorithm-in-python/63785 "2023-10-29T17:20:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Robinson](https://avatars.discourse-cdn.com/v4/letter/d/80bc00/32.png) [@Daniel\_Robinson](https://forum.anaconda.com/u/Daniel_Robinson)\
**Post date:** [October 29, 2023, 5:20pm UTC](https://forum.anaconda.com/t/simple-rules-based-grouping-algorithm-in-python/63785/1 "2023-10-29T17:20:39Z")

</div>

Hello all, I have a dataset of 150 people. I’m trying to develop a simple rules based algorithm in Python which will match these people into groups of 3 based on the rules below:

1. Same gender (male or female)
2. Similar age (plus or minus 5 years)
3. Same favourite colour
4. Same favourite food
5. Same favourite country

The algorithm should group people into 2 if there is not enough data for a 3rd person. When data is added and the algorithm is executed again the 3rd person should be added to that group.

Any help would be much appreciated!

---

<div class="post-metadata">

**Author:** ![sweller](https://avatars.discourse-cdn.com/v4/letter/s/c2df01/32.png) [@sweller](https://forum.anaconda.com/u/sweller)\
**Post date:** [November 28, 2023, 7:01pm UTC](https://forum.anaconda.com/t/simple-rules-based-grouping-algorithm-in-python/63785/2 "2023-11-28T19:01:49Z")

</div>

The way I would do this would be to write a Python function that assigns people to groups.  
You can use a ‘case’ statement and ‘if’ or ‘when’ condition to group individuals.
