[Tutor] combinations of all rows and cols from a dataframe
ThreeBlindQuarks
threesomequarks at proton.me
Wed Mar 29 21:01:06 EDT 2023
Marc,
Before answering, we need to really understand what you want and whether you need to handle more cases than you specify.
You did not show the output of your code for comparison.
My reading of what you want is you have a 2x2 grouping of numbers with a LHS and a RHS. You want what looks like this:
For each row take the LHS argument (2 and later 8) and pair it with any combination of the RHS of ALL the rows except null. I mean you want a list of combinations of 6,5 as just [6], just [5] and the combo of [6,5] but NOT the symmetric [5,6].
But then you flip it without flipping it and seem to want a similar output for
6 2
5 8
This new LHS is to be dealt with the same way by taking each row and doing the restricted combinations of everything in the LHS column.
The above is fairly easy to do if limited to your 2x2 example, perhaps with other numbers. Write a function that implements it on your 2x2 then call it again after exchanging the two columns and combine.
The code you show is complex and deeply nested with four loops. It looks like it is trying to solve a more complex case than 2x2 and I have not studied it. I suspect there is an easier and more pythonic way to do it, albeit I have no real notion why you need to massage your data in this way.
I would actually consider a two-pass version as another tack. You can take your data.frame and add a column or more that contains the required permutations, perhaps trimming out the ones you do not seem to want.
A second pass might make multiple rows out of each one, using each of the remaining permutations.
But I remain unsure of what you expect in other than the 2x2 case.
Good luck.
Sent with Proton Mail secure email.
------- Original Message -------
On Wednesday, March 29th, 2023 at 7:03 PM, marc nicole <mk1853387 at gmail.com> 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?
> _______________________________________________
> 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