简体   繁体   中英

Grouping data by value in first column

I'm trying to group data from a 2 column object based on the value of a first column. I need this data in a list so I can sort them afterwards. I am fetching interface data with snmp on large number of machines. In the example I have 2 interfaces. I need data grouped by interface preferably in a list.

Data i get is in object item:

for i in item:
   print i.oid, i.val

ifDescr lo
ifDescr eth0
ifAdminStatus 1
ifAdminStatus 1
ifOperStatus 1
ifOperStatus 0

i would like to get this data sorted in a list by value in the first column, like this:

I would like to get this data in a list, so it looks like this:

list=[[lo,1,1], [eth0,1,0]]

Solution I have is oh so dirty and long and I'm embarrassed to post it here, so any help is appreciated.

Here is my solution so you get better picture what I'm talking about. What I did is put each interface data in separate list based on item.oid, and then iterated trough cpu list and compared it to memory and name based on item.iid. In the end I have all data in cpu list where each interface is an element of the list. This solution works, but is too slow for my needs.

cpu=[]
memory=[]
name=[]

for item in process:
    if item.oid=='ifDescr':
        cpu.append([item.iid, int(item.val)])
    if item.oid=='ifAdminStatus':
        memory.append([item.iid, int(item.val)])
    if item.oid=='ifOperStatus':
        name.append([item.iid, item.val])


for c in cpu:
    for m in memory:
        if m[0]==c[0]:
            c.append(m[1])
    for n in name:
        if n[0]==c[0]:
            c.append(n[1])
cpu=sorted(cpu,key=itemgetter(1),reverse=True) #sorting is easy

Is there a pythonic, short and faster way of doing this? Limiting factor is that I get data in a 2 column object with key=data values.

Not sure I follow your sorting as I don't see any order but to group you can use a dict grouping by oid using a defaultdict for the repeating keys:

data = """ifDescr lo
ifDescr eth0
ifAdminStatus 1
ifAdminStatus 1
ifOperStatus 1
ifOperStatus 0"""

from collections import defaultdict

d = defaultdict(list)
for line in data.splitlines():
    a, b = line.split()
    d[a].append(b)
print((d.items()))
[('ifOperStatus', ['1', '0']), ('ifAdminStatus', ['1', '1']), ('ifDescr', ['lo', 'eth0'])]

using your code just use the attributes:

for i in item:
   d[i.oid].append(i.val)

Pandas is a great way to work with data. Here is a quick example code. Check out the official website for more info.

# Python script using Pandas and Numpy
from pandas import DataFrame
from numpy import random

# Data with the dictionary keys defining the columns
data_dictionary = {'a': random.random(5), 
                   'b': random.random(5)}
# Make a data frame 
data_frame = DataFrame(data_dictionary)
print(data_frame)

# Return an new data frame with a sorted first column
data_frame_sorted = data_frame.sort_index(by='a')
print(data_frame_sorted)

This should run if you have numpy an pandas installed. If you don't have any clue about installing pandas go get the "anaconda python distribution."

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM