nabs
nabs

Reputation: 21

Using itertools.combinations with columns

I have a Dataframe df with 3 columns. A,B and C

A B C
2 4 4
5 2 5
6 9 5 

My goal is to use itertools.combinations to find all non-repeating column pairs and to put the first column pair in one DataFrame and the second in the other. So all pairs of this would give A:B,A:C,B:C.

So the first dataframe df1 would have the first of of those column pairs:

df=A A B
   2 4 4
   5 5 2
   6 5 9

and the second df2:

   B C C
   4 4 4
   3 5 5
   9 5 5

I'm trying to do something with itertools like:

    for cola, colb in itertools.combinations(df, 2):
        df1[cola]=cola
        df2[colb]=colb

I know that makes no sense but i can change each column to a list and itertool a list of lists and then append each to a list A and B and then turn that list back into a Dataframe but then Im missing the headers. And I tried adding the headers to the list but when i try and remake it back to a DataFrame the indexing seems off and I cant seem to fix it. So I'm just trying to see if there is a way to just itertool entire columns with the headers.

Upvotes: 0

Views: 2244

Answers (1)

control_fd
control_fd

Reputation: 348

Utilize the zip function to group the columns to be used in each DataFrame separately, and then use pandas.concat to construct your new DataFrames:

from itertools import combinations

df1_cols, df2_cols = zip(*combinations(df.columns,2))

df1 = pd.concat([df[col] for col in df1_cols],axis=1)
df2 = pd.concat([df[col] for col in df2_cols],axis=1)

Upvotes: 1

Related Questions