Is there a more consise way to convert a pandas index to a boolean mask?

Viewed 183

I have an list of indices from a groupby operation like this:

pd.DataFrame([
        {
            'group': 'product_1',
            'value': 0.2,
        },
        {
            'group': 'product_1',
            'value': 0.3,
        },
    ])

idx = (
    df.iloc[df
            .groupby('group')
            ['value']
            .idxmin(axis=0)  # select first occurrence of min error in each group
            ]
        .index
)

I want to convert it to a boolean series/mask like this:

df_2 = df.copy()
df_2['mask'] = False
df_2.loc[idx, 'mask'] = True

which results in:

[True, False]

Is there a more consise way to perform the second (or both) steps?

1 Answers

If possible compare all minimal values per groups use GroupBy.transform and compare by original values:

df = pd.DataFrame([
        {
            'group': 'product_1',
            'value': 0.2,
        },
        {
            'group': 'product_1',
            'value': 0.2,
        },
        {
            'group': 'product_1',
            'value': 0.3,
        },
    ])

m = df.groupby('group')['value'].transform('min').eq(df['value'])
print (m)
0     True
1     True
2    False
Name: value, dtype: bool

If nned only first minimal value per groups chain masks for test first duplicated values by DataFrame.duplicated:

m1 = df.groupby('group')['value'].transform('min').eq(df['value'])
m2 = ~df.duplicated(['group','value'])
m = m1 & m2
print (m)
0     True
1    False
2    False
dtype: bool
Related