[Tutor] combinations of all rows and cols from a dataframe

Peter Otten __peter__ at web.de
Thu Mar 30 04:37:17 EDT 2023


On 30/03/2023 01:03, marc nicole wrote:
> Hello everyone,
>
> Given a dataframe like this:
>
> 2 6
> 8 5
>
> I want to yield the following list of lists:
> [  [[2],[6,5]],
> [[2],[6]],
> [[2],[5]],
> [[8],[6,5]],
> [[8],[6]],
> [[8],[5]],
> [[6],[2,8]],
> [[6],[8]],
> [[6],[2]],
> [[5],[2,8]],
> [[5],[2]],
> [[5],[8]],
> [[6,5],[2,8]]  ]
>
> I have written the following (which doesn't yield the expected results)
>
> import pandas as pd
>> from itertools import combinations
>> import numpy as np
>> resList=[]
>> resListTmp=[]
>> resListTmp2=[]
>> dataframe =
>> pd.read_excel("C:\\Users\\user\\Desktop\\testData.xlsx",index_col=False,header=None)
>
> for i in range(0, len(dataframe)+1):
>>      for j in range(0, len(dataframe.columns)):
>>          for k in range (0,len(dataframe)+1):
>>              for xVals in list(combinations(dataframe.iloc[k:i,j], i)):
>>                  if list(xVals) not in resListTmp:
>>                      resListTmp.append(list(xVals))
>>          resListTmp2.append(resListTmp)
>>      resList.append(resListTmp2)
>> print(resList)
>>
>
> What is wrong with my code?

I think you need to move the initialization of the temporary list into
the respective loop, but was unable to get it to work.

My second attempt was to start with the combinations from one column:

 >>> def colcomb(column):
	result = []
	for i in range(len(column)):
		for c in combinations(column, i+1):
			result.append(list(c))
	return result

 >>> colcomb([2, 8])
[[2], [8], [2, 8]]

But what could be the next step? I'm unable to infer it from your sample
result which seems to be somewhere between

 >>> a = [[2, 8], [6, 5]]  # transposed to avoid pandas/numpy
 >>> from itertools import product
 >>> list(product(*(colcomb(col) for col in a)))
[([2], [6]), ([2], [5]), ([2], [6, 5]), ([8], [6]), ([8], [5]), ([8],
[6, 5]), ([2, 8], [6]), ([2, 8], [5]), ([2, 8], [6, 5])]

and

 >>> x = list(product(*(colcomb(col) for col in a)))
 >>> x + [y[::-1] for y in x]
[([2], [6]), ([2], [5]), ([2], [6, 5]), ([8], [6]), ([8], [5]), ([8],
[6, 5]), ([2, 8], [6]), ([2, 8], [5]), ([2, 8], [6, 5]), ([6], [2]),
([5], [2]), ([6, 5], [2]), ([6], [8]), ([5], [8]), ([6, 5], [8]), ([6],
[2, 8]), ([5], [2, 8]), ([6, 5], [2, 8])]

Perhaps you can try and tell us what you want in plain English?



More information about the Tutor mailing list