Home > database >  Python pandas, bad output while filtering
Python pandas, bad output while filtering

Time:01-10

Hello everyone I've a little issue that i'd like to be solve (like every issues no?)

First, i've made this code :

from netmiko import ConnectHandler
from re import search
import pandas as pd
from io import StringIO
import re 


switch = []

NetworkDevice= {"host" : "A.B.C.D",
            "username" : "blabla",
            "password" : "secureblabla",
            "device_type" : "cisco_ios",}
Connect = ConnectHandler(**NetworkDevice)
Connect.enable
command = "sh int status"
result = Connect.send_command(command).strip()
print(result)
switch.append(result)

with open (r".\nst.csv","w") as filout:
   for i in switch:
     filout.write(','.join(switch))

header = re.split('\s ', result.split('\n', 1)[0])
 
df = pd.read_csv(StringIO((result)), engine="python", skiprows=2, names=header)
print(df[['Port']])

The output of this is :

Port      Name               Status       Vlan       Duplex  Speed Type 
Gi0/1                        connected    8          a-full a-1000 10/100/1000BaseTX
Gi0/2                        notconnect   29           auto   auto 10/100/1000BaseTX
Gi0/3                        notconnect   20           auto   auto 10/100/1000BaseTX
Gi0/4                        notconnect   81           auto   auto 10/100/1000BaseTX
Gi0/5                        notconnect   8            auto   auto 10/100/1000BaseTX
Gi0/6                        notconnect   8            auto   auto 10/100/1000BaseTX
Gi0/7     Borne DECT         notconnect   1            auto   auto 10/100/1000BaseTX
Gi0/8                        notconnect   1            auto   auto 10/100/1000BaseTX
Gi0/9                        notconnect   1            auto   auto Not Present
Gi0/10                       connected    trunk      a-full a-1000 10/100/1000BaseTX
                                                Port
0  Gi0/2                        notconnect   29  ...
1  Gi0/3                        notconnect   20  ...
2  Gi0/4                        notconnect   81  ...
3  Gi0/5                        notconnect   8   ...
4  Gi0/6                        notconnect   8   ...
5  Gi0/7     Borne DECT         notconnect   1   ...
6  Gi0/8                        notconnect   1   ...
7  Gi0/9                        notconnect   1   ...
8  Gi0/10                       connected    trun...

The output is close to the result I want but i can't find the solution

I'd like to have just this output (for df ofc, not result) :

Gi0/1
Gi0/2
Gi0/3
Gi0/4
Gi0/5
Gi0/6
Gi0/7
Gi0/8
Gi0/9
Gi0/10

Is there a way to do that ? I searched for many hours with tricks like df.iloc or df[:,0] but nothing is working.

If someone has a way or an idea to help Thanks in advance !

Lucas

CodePudding user response:

Sure, just extract the Series "Port" from df by merely doing df.Port. To get rid of the column name and just give back the values, cast to list.

thing_you_want = list(df.Port)
  • Related