I have a csv file where i have columns as follows:
Source Rack Switch Label/ID Switch no Switch Port
1 Hostname1 Switch1 1
with around 100 values in them. My goal is to filter the Label and see how many ports are used. Apart from that, get a count value of the number of ports used by the switch.
Using CSVreader i get the values in python but i am stuck trying to filter them. Please suggest a method to get this done.
Thanks!
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())
If I've understood correctly, you want something like this:
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
Which results in:
label switch_no switch_port
0 hostname1 Switch1 1
1 hostname1 Switch1 1
2 hostname2 Switch1 2
3 hostname2 Switch2 3
After:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.