我建议使用<代码>tertools.combination(),以获得一定长度的栏目的所有组合,并在栏目中填入。
Example:
import numpy as np
import pandas as pd
import itertools
df = pd.DataFrame({col: np.random.rand(10) for col in abcdef })
def all_combined_product_cols(df):
cols = list(df.columns)
product_cols = []
for length in range(1, len(cols) + 1):
for combination in itertools.combinations(cols, r=length):
combined_col = None
for col in combination:
if combined_col is None:
combined_col = df[col].copy()
else:
combined_col *= df[col]
combined_col.name = _ .join(combination)
product_cols.append(combined_col)
return pd.concat(product_cols, axis=1)
print(all_combined_product_cols(df))