I have a data frame called `df` of which its value counts are the following:

```
df.Priority.value_counts()

P3    39506
P2    3038 
P4    1138 
P1    1117 
P5    252  
Name: Priority, dtype: int64

```

I am trying to create a balanced dataset called `df_balanced` from `df` by restricting the number of entries in the `P3` category to 5000. The expected output should look like this!

```
P3    5000
P2    3038 
P4    1138 
P1    1117 
P5    252  
Name: Priority, dtype: int64

```

I tried the following code:

```py
s0 = df.Priority[df.Priority.eq('P3')].sample(5000).index

df_balanced = df.loc[s0.union(df)].reset_index(drop=True, inplace=True)  # I am unsure how to exclude the entries of `P3` categories from `df`!

```

I used this as a reference: [Randomly selecting rows from a dataframe based on a column value](https://stackoverflow.com/q/57405126/10543310) but the solution provided isn't optimal for more than 2 categories.