# Partition by a key

**URL:** <https://discuss.ray.io/t/partition-by-a-key/6841>\
**Category:** Ray Data\
**Created:** [July 15, 2022, 5:33pm UTC](https://discuss.ray.io/t/partition-by-a-key/6841 "2022-07-15T17:33:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Praveen](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ray.io/praveen/32/2781_2.png) [@Praveen](https://discuss.ray.io/u/Praveen)\
**Post date:** [July 15, 2022, 5:33pm UTC](https://discuss.ray.io/t/partition-by-a-key/6841/1 "2022-07-15T17:33:19Z")

</div>

**How severe does this issue affect your experience of using Ray?**

- Medium: It contributes to significant difficulty to complete my task, but I can work around it.

I am working on a pipeline which requires running a mapper after partitioning by a key. I am wondering if Ray currently provides such functionality

---

<div class="post-metadata">

**Author:** ![jianxiao](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@jianxiao](https://discuss.ray.io/u/jianxiao)\
**Post date:** [August 1, 2022, 4:59pm UTC](https://discuss.ray.io/t/partition-by-a-key/6841/2 "2022-08-01T16:59:54Z")

</div>

Hi @Praveen, Ray has a way to do it since 1.12.0 by using `.groupby(my_key)` and then `.map_groups(my_mapper_func)`.

For example:

```auto
            >>> df = pd.DataFrame(
            ... {"A": ["a", "a", "b"], "B": [1, 1, 3], "C": [4, 6, 5]}
            ... )
            >>> ds = ray.data.from_pandas(df) 
            >>> grouped = ds.groupby("A")
            >>> grouped.map_groups(
            ... lambda g: g.apply(
            ... lambda c: c / g[c.name].sum() if c.name in ["B", "C"] else c
            ... )
            ... )

```
