I have a dataframe df with 4 columms, A,B,C & D
I want to multiple every combination of these columns.
So far I have;
columns=[A,B,C,D]
a= combinations(columns)
for i in a:
df[outname]=df[a].multiply()
but obviously this isn't correct.
Can anyone see a good way?
Output:
A B C D AB AC AD BC ABC and so on
0
1
2
3
4
6
7
Use function from this for find all combinations and in list comprehension create all product of values:
df = pd.DataFrame({
'A':[5,3,6,9,2,4],
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
})
from itertools import chain, combinations
def all_subsets(ss):
return chain(*map(lambda x: combinations(ss, x), range(1, len(ss)+1)))
#get all combination
tups = list(all_subsets(df.columns))
#for each combination multiple values
df1 = pd.concat([df.loc[:,c].product(axis=1) for c in tups], axis=1)
#set new columns by join list of tuples tups
df1.columns = [''.join(x) for x in tups]
print (df1)
A B C D AB AC AD BC BD CD ABC ABD ACD BCD ABCD
0 5 4 7 1 20 35 5 28 4 7 140 20 35 28 140
1 3 5 8 3 15 24 9 40 15 24 120 45 72 120 360
2 6 4 9 5 24 54 30 36 20 45 216 120 270 180 1080
3 9 5 4 7 45 36 63 20 35 28 180 315 252 140 1260
4 2 5 2 1 10 4 2 10 5 2 20 10 4 10 20
5 4 4 3 0 16 12 0 12 0 0 48 0 0 0 0
Use:
import itertools
L=[(x, y) for x, y in itertools.product(df.columns,df.columns) if x != y]
pd.concat([pd.DataFrame({''.join(i):df.loc[:,i].prod(axis=1)}) for i in L],axis=1)
AB AC AD BA BC BD CA CB CD DA DB DC
0 20 35 5 20 28 4 35 28 7 5 4 7
1 15 24 9 15 40 15 24 40 24 9 15 24
2 24 54 30 24 36 20 54 36 45 30 20 45
3 45 36 63 45 20 35 36 20 28 63 35 28
4 10 4 2 10 10 5 4 10 2 2 5 2
5 16 12 0 16 12 0 12 12 0 0 0 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With