【发布时间】:2019-09-02 15:37:32
【问题描述】:
早上好,我有一个关于 pandas 和 python 的非常重要的问题。我是 Python 的新手,我正在使用 PARAMIKO 库,通过它我得到了一个类似于表格的“形式”的答案,但我想应用一个数据框并更好地获得对我最有用的列“interfaces”“InUti”和“OutUti”的值,问题是我正在阅读熊猫,但我不知道如何应用熊猫的某些属性来获得我从我的字符串中获得的数据框。然后我留下我从答案中得到的字符串,我需要应用数据框。如上所述,我只对“Interface”“InUti”“OutUti”列感兴趣。
GigabitEthernet7 / 1/0 (10G), GigabitEthernet16 / 1/0 (10G) 等名称开头的空格默认是这样的,所以必须考虑在内。
我正在阅读有关 pandas 的内容,但我没有得到或多或少适合我需要的东西,但如果您知道任何有趣的文档,我将不胜感激
print(data)
Interface PHY Protocol InUti OutUti inErrors outErrors
40GE7/0/0 up up 6.97% 14.85% 0 0
40GE16/0/0 up up 25.69% 0.75% 0 0
Eth-Trunk1 up up 18.55% 10.07% 506 0
GigabitEthernet7/1/0(10G) up up 17.61% 10.16% 222 0
GigabitEthernet16/1/0(10G) up up 19.49% 9.97% 284 0
Eth-Trunk8 up up 39.19% 46.10% 0 0
GigabitEthernet7/1/9(10G) up up 39.80% 46.09% 0 0
GigabitEthernet16/1/9(10G) up up 38.58% 46.11% 0 0
GigabitEthernet0/0/0 up up 0.01% 0.01% 0 0
GigabitEthernet1/2/0 up up 0.04% 0.01% 0 0
GigabitEthernet1/2/3 up up 0.01% 9.67% 0 0
GigabitEthernet1/2/6 up up 0.01% 0.01% 0 0
GigabitEthernet1/2/7 up up 13.94% 26.52% 0 0
GigabitEthernet1/2/8 up up 0.23% 0.01% 0 0
GigabitEthernet1/2/11 up up 0.39% 5.34% 0 0
GigabitEthernet1/2/12 up up 1.10% 4.09% 0 0
GigabitEthernet1/2/13 up up 21.65% 7.33% 0 0
GigabitEthernet1/2/15 up up 0.23% 4.76% 0 0
GigabitEthernet1/2/16 up up 5.10% 13.55% 0 0
GigabitEthernet7/1/6(10G) up up 4.23% 5.71% 0 0
GigabitEthernet7/1/7(10G) up up 4.48% 13.07% 0 0
GigabitEthernet7/1/11(10G) up up 0.92% 4.56% 0 0
GigabitEthernet7/1/12(10G) up up 4.53% 16.12% 0 0
GigabitEthernet7/1/13(10G) up up 6.43% 17.46% 0 0
GigabitEthernet16/1/7(10G) up up 2.85% 8.15% 0 0
GigabitEthernet16/1/11(10G) up up 6.75% 19.73% 0 0
GigabitEthernet16/1/12(10G) up up 0.01% 12.43% 0 0
LoopBack150 up up(s) 0% 0% 0 0
LoopBack160 up up(s) 0% 0% 0 0
LoopBack170 up up(s) 0% 0% 0 0
LoopBack199 up up(s) 0% 0% 0 0
LoopBack200 up up(s) 0% 0% 0 0
NULL0 up up(s) 0% 0% 0 0
[更新]: 除了同伴@crayxt 提出的解决方案之外,我还实现了一个解决方案,它包括使用pandas.read_fwf,代码(针对我的问题)如下:
df = pd.read_fwf (io.StringIO (data), widths = [27,4,11,8,9,8,11,10])
print (df)
print (df.loc [0])
Interface 40GE7/0/0
PHY up
Protocol up
InUti 7.60%
OutUti 14.95%
inErrors 0
outErrors 0
Unnamed: 7 NaN
Name: 0, dtype: object
【问题讨论】:
标签: python arrays pandas paramiko