Python pandas,筛选时输出不佳。

huangapple go评论64阅读模式
英文:

Python pandas, bad output while filtering

问题

你好,以下是您要求的代码部分的翻译:

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

请注意,以上内容是您提供的代码的翻译,不包括问题的回答或其他内容。

英文:

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

答案1

得分: 0

提取DataFrame df 中的Series "Port",只需使用df.Port。要去掉列名,只返回值,将其转换为列表。

thing_you_want = list(df.Port)
英文:

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)

huangapple
  • 本文由 发表于 2023年1月9日 18:57:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75056287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定