Creating new columns in pandas dataframe as summed permutations of other columns
Use combinations
instead permutations
and for each values of tuple sum values together:
from itertools import combinations
cc = list(combinations(df.columns,3))
df = pd.concat([df.loc[:, c].sum(axis=1) for c in cc], axis=1, keys=cc)
df.columns = df.columns.map(''.join)
print (df)
ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE
0 7 9 7 11 9 11 12 10 12 14
1 8 9 10 6 7 8 7 8 9 6
2 15 12 15 9 12 9 12 15 12 9
Using the underlying numpy array for efficiency.
from itertools import combinations
a = df.to_numpy()
df2 = pd.DataFrame(np.vstack([a[:,list(c)].sum(1)
for c in combinations(range(df.shape[1]), 3)
]).T,
columns=map(''.join, combinations(df, 3))
)
output:
ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE
0 7 9 7 11 9 11 12 10 12 14
1 8 9 10 6 7 8 7 8 9 6
2 15 12 15 9 12 9 12 15 12 9
NB. This is running ~10 times faster than the other pandas-based solution.
Or as an alternative:
import itertools
pd.DataFrame({'{}{}{}'.format(a, b, c): df[a] + df[b] + df[c]
for a, b, c in itertools.combinations(df.columns, 3)})
Output:
ABC ABD ABE ACD ACE ADE BCD BCE BDE CDE
0 7 9 7 11 9 11 12 10 12 14
1 8 9 10 6 7 8 7 8 9 6
2 15 12 15 9 12 9 12 15 12 9