Lost in Pandas - Part 4

Data Science
Author

Nodar Okroshiashvili

Published

April 29, 2020

Introduction

I’ll show how to assign new value for each unique group in Pandas DataFrame.

import pandas as pd
data = {
    "ID": [1, 1, 1, 2, 2, 3, 3],
    "class": ["Lower", "Moderate", "Moderate", "Lower", "High", "High", "Lower"]
}

df = pd.DataFrame(data)

df
ID class
0 1 Lower
1 1 Moderate
2 1 Moderate
3 2 Lower
4 2 High
5 3 High
6 3 Lower

Now, let assign new values according to class column and then rearrange class column in order to assign new group names.

value_map = {"Lower": 33, "Moderate": 34, "High": 40}

df["new"] = df["class"].map(value_map)

df
ID class new
0 1 Lower 33
1 1 Moderate 34
2 1 Moderate 34
3 2 Lower 33
4 2 High 40
5 3 High 40
6 3 Lower 33
df["Max"] = (df.set_index("class")
                .groupby("ID")["new"]
                .transform("idxmax").values)

df
ID class new Max
0 1 Lower 33 Moderate
1 1 Moderate 34 Moderate
2 1 Moderate 34 Moderate
3 2 Lower 33 High
4 2 High 40 High
5 3 High 40 High
6 3 Lower 33 High
df["Max"] = df["Max"].mask(df["ID"].duplicated(), "")

df
ID class new Max
0 1 Lower 33 Moderate
1 1 Moderate 34
2 1 Moderate 34
3 2 Lower 33 High
4 2 High 40
5 3 High 40 High
6 3 Lower 33