我有一个csv文件,其中我有以下列:
Source Rack Switch Label/ID Switch no Switch Port
1 Hostname1 Switch1 1
其中包含大约100个值。我的目标是过滤标签并查看使用了多少端口。 除此之外,获取交换机使用的端口数的计数值。
使用CSVreader我在python中获取值,但我试图过滤它们。 请建议一种方法来完成这项工作。
谢谢!
import pandas as pd
import csv
import numpy
import matplotlib
#import datetime
#import pandas.io.data
data_df = pd.read_csv('patchingwlan.csv',index_col = 1)
data_df.filter(items=['Hostname','Switch Port'])
print(data_df.head())
答案 0 :(得分:0)
如果我理解正确,你需要这样的东西:
import pandas as pd
pd.set_option("display.width", 300)
# Test input data
df = pd.DataFrame({
"label": ["hostname1", "hostname1", "hostname2", "hostname2"],
"switch_no": ["Switch1", "Switch1", "Switch1", "Switch2"],
"switch_port": [1, 1, 2, 3]
})
print df
# Count ports per label and ports per switch_no (unique and total, depending on what you want)
df["unique_ports_per_label"] = df.groupby("label")["switch_port"].transform("nunique")
df["ports_per_label"] = df.groupby("label")["switch_port"].transform(len)
df["unique_ports_per_switch"] = df.groupby("switch_no")["switch_port"].transform("nunique")
df["ports_per_switch"] = df.groupby("switch_no")["switch_port"].transform(len)
print df
结果是:
label switch_no switch_port
0 hostname1 Switch1 1
1 hostname1 Switch1 1
2 hostname2 Switch1 2
3 hostname2 Switch2 3
后:
label switch_no switch_port unique_ports_per_label ports_per_label unique_ports_per_switch ports_per_switch
0 hostname1 Switch1 1 1 2 2 3
1 hostname1 Switch1 1 1 2 2 3
2 hostname2 Switch1 2 2 2 2 3
3 hostname2 Switch2 3 2 2 1 1