Topological sort



    Rosie Djurovic

    The code first creates a dictionary called zip, which maps colA to colB. Next, it groups the data by colB, and then uses the agg function to calculate the average value for each group. Lastly, it creates a graph object and uses the toposort_flatten function to sort the data.

    import toposort
    #groupby colA
    bar = df1.groupby('colA')['colB'].agg(list).to_dict()
    graph = dict(zip(bar.keys(), map(set, bar.values())))
    #topological sort
    sorted_graph = toposort_flatten(graph, sort=True)
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.