在数据框中生成7列的所有组合,并添加相应的行以生成新的列 [英] Generating all the combinations of 7 columns in a dataframe and add the corresponding rows to generate new columns
问题描述
我有一个与下面类似的数据框:
I have a dataframe that looks similar to below:
Wave A B C
340 77 70 15
341 80 73 15
342 83 76 16
343 86 78 17
我想生成将包含现有列的所有可能组合的列.我在这里显示了3个列,但在我的实际数据中,我有7列,因此共有127种组合.所需的输出如下:
I want to generate columns that will have all the possible combinations of the existing columns. I showed 3 cols here but in my actual data, I have 7 columns and therefore 127 total combinations. The desired output is as follows:
Wave A B C AB AC AD BC ... ABC
340 77 70 15 147 92 ...
341 80 73 15 153 95 ...
342 83 76 16 159 99 ...
我实现了一个效率很低的版本,用户输入组合(AB,AC等),并使用行的总和创建一个新的col.对于127种组合(尤其是具有描述性的列名),这似乎几乎是不可能的.
I implemented a quite inefficient version where the user inputs the combinations (AB, AC, etc.) and a new col is created with the sum of the rows. This seems almost impossible to accomplish for 127 combinations, esp with descriptive col names.
推荐答案
从itertools中创建包含 chain
+ combinations
的所有组合的列表,然后对相应的列求和:
Create a list of all combinations with chain
+ combinations
from itertools, then sum the appropriate columns:
from itertools import combinations, chain
cols = [*df.iloc[:,1:]]
l = list(chain.from_iterable(combinations(cols, n+2) for n in range(len(cols))))
#[('A', 'B'), ('A', 'C'), ('B', 'C'), ('A', 'B', 'C')]
for items in l:
df[''.join(items)] = df.loc[:, items].sum(1)
Wave A B C AB AC BC ABC
0 340 77 70 15 147 92 85 162
1 341 80 73 15 153 95 88 168
2 342 83 76 16 159 99 92 175
3 343 86 78 17 164 103 95 181
这篇关于在数据框中生成7列的所有组合,并添加相应的行以生成新的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!