PythonPandas,过滤时输出错误

r3i60tvu  于 2023-01-10  发布在  Python
关注(0)|答案(1)|浏览(95)

大家好,我有一个小问题,我想得到解决(像每一个问题没有?)
首先,我做了这个代码:

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']])

其输出为:

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...

输出接近我想要的结果,但我找不到解决方案
我只希望有这样的输出(对于df ofc,而不是result):

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

有没有办法做到这一点?我用df.ilocdf[:,0]这样的技巧搜索了很多个小时,但都不起作用。
如果有人有办法或想法可以帮忙,先谢了!
卢卡斯

sz81bmfz

sz81bmfz1#

当然,只需要执行df.Port就可以从df中提取Series "Port"。要去掉列名并给予值,请强制转换为list。

thing_you_want = list(df.Port)

相关问题