这是一个低效的简单实现:)
df = pd.DataFrame({'col1':[1, 3, 5, 7], 'col2':[2, 4, 6, 8], 'col3':[9, 10, 11, 12]})
def get_all_combinations(dataframe, length):
assert (len(dataframe.columns)>= length) & (len(dataframe)>= length)
cols = list(itertools.combinations(dataframe.columns, length))
rows = list(itertools.combinations(range(len(dataframe)), length))
for col in cols:
for row in rows:
temp_df = dataframe.loc[row, col]
print([temp_df.iloc[idx, idx] for idx in range(length)])
get_all_combinations(df, 2)
输出:
[1, 4]
[1, 6]
[1, 8]
[3, 6]
[3, 8]
[5, 8]
[1, 10]
[1, 11]
[1, 12]
[3, 11]
[3, 12]
[5, 12]
[2, 10]
[2, 11]
[2, 12]
[4, 11]
[4, 12]
[6, 12]