Search code examples
pythonpandasdatasetnumericcategorical

Convert numerical to categorical in python pandas


I have a dataset of internet traffic. There is a ports column I want to convert ports to categorical. This is the code I written to it

df2.loc[df2['Src Port'] == 443] = 'HTTPS'

Now I want to category all rests ports into category called 'other'. So how to do that?


Solution

  • I would use a dictionary and map:

    df2['Src Port'] = df2['Src Port'].map({443: 'HTTPS'}).fillna('other')
    

    Alternatively, the obvious complement to your approach would be:

    df2.loc[df2['Src Port'] != 443] = 'other'
    

    NB. you assign to all columns here!