Python pandas,筛选时输出不佳。

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

Python pandas, bad output while filtering

问题

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

  1. from netmiko import ConnectHandler
  2. from re import search
  3. import pandas as pd
  4. from io import StringIO
  5. import re
  6. switch = []
  7. NetworkDevice = {"host": "A.B.C.D",
  8. "username": "blabla",
  9. "password": "secureblabla",
  10. "device_type": "cisco_ios", }
  11. Connect = ConnectHandler(**NetworkDevice)
  12. Connect.enable
  13. command = "sh int status"
  14. result = Connect.send_command(command).strip()
  15. print(result)
  16. switch.append(result)
  17. with open(r".\nst.csv", "w") as filout:
  18. for i in switch:
  19. filout.write(','.join(switch))
  20. header = re.split('\s+', result.split('\n', 1)[0])
  21. df = pd.read_csv(StringIO((result)), engine="python", skiprows=2, names=header)
  22. 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 :

  1. from netmiko import ConnectHandler
  2. from re import search
  3. import pandas as pd
  4. from io import StringIO
  5. import re
  6. switch = []
  7. NetworkDevice= {"host" : "A.B.C.D",
  8. "username" : "blabla",
  9. "password" : "secureblabla",
  10. "device_type" : "cisco_ios",}
  11. Connect = ConnectHandler(**NetworkDevice)
  12. Connect.enable
  13. command = "sh int status"
  14. result = Connect.send_command(command).strip()
  15. print(result)
  16. switch.append(result)
  17. with open (r".\nst.csv","w") as filout:
  18. for i in switch:
  19. filout.write(','.join(switch))
  20. header = re.split('\s+', result.split('\n', 1)[0])
  21. df = pd.read_csv(StringIO((result)), engine="python", skiprows=2, names=header)
  22. print(df[['Port']])

The output of this is :

  1. Port Name Status Vlan Duplex Speed Type
  2. Gi0/1 connected 8 a-full a-1000 10/100/1000BaseTX
  3. Gi0/2 notconnect 29 auto auto 10/100/1000BaseTX
  4. Gi0/3 notconnect 20 auto auto 10/100/1000BaseTX
  5. Gi0/4 notconnect 81 auto auto 10/100/1000BaseTX
  6. Gi0/5 notconnect 8 auto auto 10/100/1000BaseTX
  7. Gi0/6 notconnect 8 auto auto 10/100/1000BaseTX
  8. Gi0/7 Borne DECT notconnect 1 auto auto 10/100/1000BaseTX
  9. Gi0/8 notconnect 1 auto auto 10/100/1000BaseTX
  10. Gi0/9 notconnect 1 auto auto Not Present
  11. Gi0/10 connected trunk a-full a-1000 10/100/1000BaseTX
  12. Port
  13. 0 Gi0/2 notconnect 29 ...
  14. 1 Gi0/3 notconnect 20 ...
  15. 2 Gi0/4 notconnect 81 ...
  16. 3 Gi0/5 notconnect 8 ...
  17. 4 Gi0/6 notconnect 8 ...
  18. 5 Gi0/7 Borne DECT notconnect 1 ...
  19. 6 Gi0/8 notconnect 1 ...
  20. 7 Gi0/9 notconnect 1 ...
  21. 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) :

  1. Gi0/1
  2. Gi0/2
  3. Gi0/3
  4. Gi0/4
  5. Gi0/5
  6. Gi0/6
  7. Gi0/7
  8. Gi0/8
  9. Gi0/9
  10. 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。要去掉列名,只返回值,将其转换为列表。

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

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

确定