0

I am working in Ipython and trying to generate all the permutations of the columns present in the dataframe. This issue is that I have about 15 columns in my dataframe and the code just doesn't reach end and keeps on executing. Here is my current code:

df = pd.read_csv(filename, sep = ';', error_bad_lines=False)

def all_perms(str):
    if len(str) <=1:
        yield str
    else:
        for perm in all_perms(str[1:]):
            for i in range(len(str)):
                #nb str[0:1] works in both string and list contexts
                yield perm[:i] + str[0:1] + perm[i:]

allperms_list = []
for p in all_perms(df.columns[:len(df.columns)]):
    allperms_list.append(p) 

I followed this SO example. I have a 16 core and 32GB memory system and have been running this code for last 1.5 hours but its still executing and doesn't look like reaching an end. Is there some fundamental issue in the code that I have? How can I make this to run faster without affecting final result? I read that itertools.permutations is another option. How can I modify my current code to include itertools.permutations and achieve the same result?

Community
  • 1
  • 1
user2966197
  • 2,793
  • 10
  • 45
  • 77
  • 1
    `from itertools import permutations;for p in permutations(df.columns)):print p`, how many columns do you have? – Padraic Cunningham Jan 16 '16 at 01:07
  • @PadraicCunningham I have 15 columns in my dataframe – user2966197 Jan 16 '16 at 01:12
  • That should not be running for 1.5 hours – Padraic Cunningham Jan 16 '16 at 01:13
  • @PadraicCunningham Yes thats why I thought there is something wrong that I am doing in my code – user2966197 Jan 16 '16 at 01:20
  • @PadraicCunningham is there anything wrong that you can se in my code above? And is using `itertools.permutations` better that the above approach in terms in speed (in terms of complexity it definitely looks better to have a two line code in itertools.permutations than my approach)? – user2966197 Jan 16 '16 at 01:26
  • itertools is implemented in c so it is optimized in that sense, there are still n! possible permutations however you approach it. I cannot see any reason in your code, have you added some prints in the loop to see what is happening? – Padraic Cunningham Jan 16 '16 at 01:39

0 Answers0