2

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?

1 Answers1

1

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!

mozway
  • 194,879
  • 13
  • 39
  • 75