'How to create a condition for the future input variables in python

I have a dataframe like the below:

df1:
Pan_no.     Last_broker_cat
Xxx             Mutual fund
Yyy            National distributor
ZZZ            National Distributor
Aaa              Debt champion
BBB            National distributor
Ccc              Debt champion

I am mapping each value of Last_broker_cat column to an unique number :

df1['Last_broker_cat] = df1['Last_broker_cat].map({'National distributor':1,'Mutual fund':2, 'debt_champion :3})

Now my df1 looks like the below:

df1

Pan_no.     Last_broker_cat
Xxx            2
Yyy            1
ZZZ            1
Aaa            3
BBB            1
Ccc            3

Now I have a condition:

In the future input variable, if there are any new value in the Last_broker_cat column apart from the existing one I need to assign it with the unique number that I have assigned to the least occuring value in the dataframe. Eg in our dataframe the least occuring value is 2 so any new value that comes in the future should be assigned with the least value. How can I code this condition in python?



Solution 1:[1]

Try this

df['Last_broker_cat'] = pd.factorize(df['Last_broker_cat'])[0]

But the unique values start from 0 to n unique values.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 pyaj