I am struggling to find some elegant solution to get what I need from my data. I am able to get what I want but with too much efforts, which I believe can be done quite better and that's what I am looking for.
So here is sample of my DataFrame
>>> df = pd.DataFrame({'device_name': ['tap_switch_1', 'tap_switch_1', 'tap_switch_1', 'tap_switch_1', 'tap_switch_1', 'tap_switch_1', 'tap_switch_1', 'tap_switch_1', 'tap_switch_1'], 'interface': ['ethernet3', 'ethernet4', 'ethernet38', 'ethernet7', 'ethernet8', 'ethernet31', 'ethernet1', 'ethernet12', 'ethernet20'], 'tap_port': ['1-tx-a-rx', '1-tx-b-rx', '1-b', '2-tx-a-rx', '2-tx-b-rx', '2-b', '3-tx-a-rx', '3-tx-b-rx', '3-b'], 'switch_name': ['sw_ag1', 'sw_ag1', 'sw_client1', 'sw_ag1', 'sw_ag1', 'sw_client2', 'sw_ag1', 'sw_ag1', 'sw_client3']})
Current DataFrame shape:
device_name interface tap_port switch_name
tap_switch_1 ethernet3 1-tx-a-rx sw_ag1
tap_switch_1 ethernet4 1-tx-b-rx sw_ag1
tap_switch_1 ethernet38 1-b sw_client1
tap_switch_1 ethernet7 2-tx-a-rx sw_ag1
tap_switch_1 ethernet8 2-tx-b-rx sw_ag1
tap_switch_1 ethernet31 2-b sw_client2
tap_switch_1 ethernet1 3-tx-a-rx sw_ag1
tap_switch_1 ethernet12 3-tx-b-rx sw_ag1
tap_switch_1 ethernet20 3-b sw_client3
The desired output I am looking to get is this:
device_name id agg_switch rx_int tx_int client_switch client_port
tap_switch_1 1 sw_ag1 ethernet3 ethernt4 sw_client1 ethernet38
tap_switch_1 2 sw_ag1 ethernet7 ethernt8 sw_client2 ethernet31
tap_switch_1 3 sw_ag1 ethernet1 ethernt12 sw_client3 ethernet20
The logic
So basically I have network setup where one switch is used to tap multiple interfaces. For every client switch port there are two device_name interfaces - one for each direction (RX/TX). I am using tap_port name to combine all that into one group, based on first integer I see, which indicates the "tap_group".
Current "solution"
Here is the way I am doing it now, which I don't quite and it doesn't give me the desired output:
# Add new `id` column
>>> df['id']=df.tap_port.str[0]
# Get RX/TX direction as new column `direction`
>>> df['direction']=df.tap_port.apply(lambda x: x[-4:] if 'x' in x else '-')
# Trying to get the desired output
>>> df.pivot(index='id', columns='direction')[['switch_name','interface']]
switch_name interface
direction - a-rx b-rx - a-rx b-rx
id
1 sw_client1 sw_ag1 sw_ag1 ethernet38 ethernet3 ethernet4
2 sw_client2 sw_ag1 sw_ag1 ethernet31 ethernet7 ethernet8
3 sw_client3 sw_ag1 sw_ag1 ethernet20 ethernet1 ethernet12
This is very close to what I need, but not quite as per the desired output.
Many thanks in advance for your help!