Issue
I have this data frame:
df
Campus ServerName Port
NA1 ServerABC Ethernet1
NA1 ServerABC Ethernet11
NA1 ServerABC Ethernet12
NA1 ServerABC Ethernet2
NA1 ServerABC Ethernet56
NA1 ServerABC Ethernet6
I would like to sort this data frame based on Campus, ServerName and Port on Ascending order. Since Port is text, I need to grab numbers from port and Sort it based on the number part of the port.
final df should be this:
Campus ServerName Port
NA1 ServerABC Ethernet1
NA1 ServerABC Ethernet2
NA1 ServerABC Ethernet6
NA1 ServerABC Ethernet11
NA1 ServerABC Ethernet12
NA1 ServerABC Ethernet56
I tried this:
df=df.sort_values(by=['Campus', 'ServerName', 'Port']
This is not giving me the result as I want as the Port numbers is not being ordered.
How could I extract the numbers from the port and sort it based on integers?
Solution
Two keys, extract port_num
with regex, then convert to int
(because string functions return strings), then sort. No reason to call apply
here since str.extract
is vectorized
import pandas as pd
df['port_num'] = df['Port'].str.extract('(\d+)').astype('int')
df.sort_values(by=['Campus', 'ServerName', 'port_num'])
Campus ServerName Port port_num
0 NA1 ServerABC Ethernet1 1
1 NA1 ServerABC Ethernet2 2
2 NA1 ServerABC Ethernet6 6
3 NA1 ServerABC Ethernet11 11
4 NA1 ServerABC Ethernet12 12
5 NA1 ServerABC Ethernet56 56
Answered By - Mako212
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.