Reputation: 160
I'm new to Python so excuse me. Back when I was into PHP/MySQL. I could loop through a table based on any parameter. Let's just say ID for simplification. Once you find the ID you can put each column into a variable. Easy...
So I created a CSV and loading into a dataframe with Pandas.
switch_id,vlan_num,vlan_desc,int_vlan,int_vlan_ip,int_vlan_mask
1,20,Legal,20,192.168.20.1,255.255.255.0
1,21,HumanResources,21,192.168.21.1,255.255.255.0
2,20,Legal,20,192.168.20.2,255.255.255.0
2,21,HumanResources,21,192.168.21.2,255.255.255.0
The code I'm using looks like this as a test to understand the functionality.
import pandas as pd
df = pd.read_csv('/Users/thenk83/Desktop/PythonCode/vlan_database.txt')
for i in df:
print(df[i][2])
The option is this:
1
22
Finance
22
192.168.22.1
255.255.255.0
How do I make it so that I can use, row['switch_id'],row['vlan_num'], etc, etc. Basically, I want to put each column in the row into a variable. I want to call out each variable myself. I don't want to put out the entire row in one variable. I can see how the row is called out but how do I select the column. So confused.
Basically, I'm wanting to configure multiple Cisco switches. Looping through each row in the dataframe and plugging in data into the configs where it's needed.
I could do this easily with Php and MySQL but it's not the same thing. So I'm confused as how to do this.
I'd prefer nothing complex. As simple as possible would be great if you can help me. I'd like to understand.
Upvotes: 1
Views: 205
Reputation: 1610
Since you are not doing any kind of mathematical or statistical operation on your data, why don't you use the csv
module. For example, based on your code:
Input_file.csv
switch_id,vlan_num,vlan_desc,int_vlan,int_vlan_ip,int_vlan_mask
1,20,Legal,20,192.168.20.1,255.255.255.0
1,21,HumanResources,21,192.168.21.1,255.255.255.0
2,20,Legal,20,192.168.20.2,255.255.255.0
2,21,HumanResources,21,192.168.21.2,255.255.255.0
Answer.py
import csv
input_file_name = "Input_file.csv"
with open(input_file_name, newline='') as input_file:
csv_reader = csv.DictReader(input_file)
for row in csv_reader:
print(row['vlan_num'],
row['vlan_desc'],
row['int_vlan'],
row['int_vlan_ip'],
row['int_vlan_mask'])
Upvotes: 0
Reputation: 18647
IIUC, I think you're looking for DataFrame.iterrows
.
This will give the same output as your solution:
value_list = ['1']
df = df[df.switch_id.isin(value_list)]
for idx, row in df.iterrows():
print(row['vlan_num'],
row['vlan_desc'],
row['int_vlan'],
row['int_vlan_ip'],
row['int_vlan_mask'])
Upvotes: 1
Reputation: 160
I figured it out after some more trial and error. I ended up using ".iloc".
import pandas as pd
df = pd.read_csv('/Users/thenk83/Desktop/PythonCode/vlan_database.txt')
value_list = ['1']
df = df[df.switch_id.isin(value_list)]
for i in range(len(df)):
vlan_num = df.iloc[i,1]
vlan_name = df.iloc[i,2]
int_vlan = df.iloc[i,3]
int_vlan_ip = df.iloc[i,4]
int_vlan_mask = df.iloc[i,5]
print(vlan_num,vlan_name,int_vlan,int_vlan_ip,int_vlan_mask)
It's probably not the cleanest way to do it but it'll work for now. But it gives me this:
(20, 'Legal', 20, '192.168.20.1', '255.255.255.0')
(21, 'HumanResources', 21, '192.168.21.1', '255.255.255.0')
(22, 'Finance', 22, '192.168.22.1', '255.255.255.0')
(23, 'Facilities', 23, '192.168.23.1', '255.255.255.0')
(24, 'InformationTechnology', 24, '192.168.24.1', '255.255.255.0')
(25, 'Engineering', 25, '192.168.25.1', '255.255.255.0')
And is exactly what I wanted!
Upvotes: 0