Transformations by group

Open
#460 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
pandas, python

Research direction

Start by reading the project’s existing feature-engineering transformer documentation and tests to see how new transformations fit its API. The issue gives a pandas groupby-transform example but does not specify the interface or supported summaries. Done means users can create group-level summary variables that are repeated for each row in the group.

Written by the indexing model from the issue text.

Description

duplicate

Is there support for creating new variables that are summaries of existing variables by group? I can't seem to find it.

For example:

import pandas as pd

df = pd.DataFrame({
    'group': ['A', 'A', 'B', 'B', 'B', 'A'],
    'x1': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6],
    'x2': [0.7, 0.8, 0.9, 0.1, 0.2, 0.3]
})

# new variables: mean of x1 and x2 by group
df[["x1_mean", "x2_mean"]] = df.groupby('group')[['x1', 'x2']].transform("mean")
print(df)
  group   x1   x2  x1_mean  x2_mean
0     A  0.1  0.7      0.3      0.6
1     A  0.2  0.8      0.3      0.6
2     B  0.3  0.9      0.4      0.4
3     B  0.4  0.1      0.4      0.4
4     B  0.5  0.2      0.4      0.4
5     A  0.6  0.3      0.3      0.6

This type of feature engineering is usually helpful.

Dominant language
Python
Stars
2.3k
Forks
391
Avg merge
11h 34m
Merged PRs (30d)
11

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from feature-engine/feature_engine

All issues in feature-engine/feature_engine

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.