如何摆脱这个groupby电话?

时间:2018-09-20 23:16:12

标签: python dataset

如何摆脱groupby()的通话?

我想摆脱对groupby的理解,并重新编写单词,以便没有groupby的导入itertools

def quartiles(numbers):
    numbers.sort()
    splitter = (median(numbers),)
    Q1A, Q3A = [list(g) for k, g in groupby(numbers, lambda x: x in splitter) if not k]
    return median(Q1A), median(Q3A)

# finds the IQR
def IQR(numbers):
    Q1, Q3 = quartiles(numbers)
    return Q3 - Q1

# makes the fences
def fences(numbers):
    Q1, Q3 = quartiles(numbers)
    IQRV = IQR(numbers)
    return (Q1 - (1.5 * IQRV)), (Q3 + (1.5 * IQRV))

# used a string and in the string is a list comprehension to print the outliers
heartweights = [row[2] for row in data]
LF, UP = fences(heartweights)
print('There are ' + str(len([i for i in heartweights if i < LF or i > UP])) + ' abnormal heart weight(s).')

1 个答案:

答案 0 :(得分:0)

如果我不得不摆脱多余的导入,我会从docs借用groupby()的大致等效定义。您可以成功摆脱导入,而不必触摸(可能破坏)任何现有代码。

class groupby(object):
    # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
    # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
    def __init__(self, iterable, key=None):
        if key is None:
            key = lambda x: x
        self.keyfunc = key
        self.it = iter(iterable)
        self.tgtkey = self.currkey = self.currvalue = object()
    def __iter__(self):
        return self
    def next(self):
        while self.currkey == self.tgtkey:
            self.currvalue = next(self.it)    # Exit on StopIteration
            self.currkey = self.keyfunc(self.currvalue)
        self.tgtkey = self.currkey
        return (self.currkey, self._grouper(self.tgtkey))
    def _grouper(self, tgtkey):
        while self.currkey == tgtkey:
            yield self.currvalue
            self.currvalue = next(self.it)    # Exit on StopIteration
            self.currkey = self.keyfunc(self.currvalue)