How to summarize on different groupby combinations?

Since your data seem to guarantee 3 unique crops per country (“I am compiling a table of top-3 crops by county.”), it suffices to sort the values and assign back. import numpy as np cols = [‘Crop1’, ‘Crop2’, ‘Crop3’] df1[cols] = np.sort(df1[cols].to_numpy(), axis=1) County Crop1 Crop2 Crop3 Total_pop 0 Harney apples grain melons 2000 1 … Read more

Prevent memory error in itertools.permutation

Try to use the iterator generated by the permutations instead of recreating a list with it : perm_iterator = itertools.permutations(list(graph.Nodes)) for item in perm_iterator: do_the_stuff(item) by doing this, python will keep in memory only the currently used permutation, not all the permutations (in term of memory usage, it is really better 😉 ) On the … Read more