[Tutor] combinations of all rows and cols from a dataframe
marc nicole
mk1853387 at gmail.com
Thu Mar 30 07:41:09 EDT 2023
@Peter what you did is more or less what i was looking for except that i
see "duplicate" tuples e.g., ([2], [6, 5]) & ([6, 5], [2]) which are
unwanted in the final output
Le jeu. 30 mars 2023 à 10:38, Peter Otten <__peter__ at web.de> a écrit :
> 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?
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list