Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
318 views
in Technique[技术] by (71.8m points)

python - How to create all combinations column wise for multiple variables in pandas?

For a given range for n variables. I have taken n=3 as an example.

A : [1,3]
B:  [5,10,12]
C: [100,113]

Note the values in the above range can be float as well.

How can we create a dataframe where every column represent a unique combination of the input variables?

    c1  c2  c3  c4  c5  c6  c7  c8  c9  c10 c11 c12
a   1   1   1   3   3   3   1   1   1   3   3   3
b   5   10  12  5   10  12  5   10  12  5   10  12
c   100 100 100 100 100 100 113 113 113 113 113 113
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using Itertools.product I can make all combinations of a list, afterwards you can write each combination to your DataFrame

import itertools

A = [1, 3]
B = [5, 10, 12]
C = [100, 113]

a = [A, B, C]

print(list(itertools.product(*a)))
# Outputs [(1, 5, 100), (1, 5, 113), (1, 10, 100), (1, 10, 113), (1, 12, 100), (1, 12, 113), (3, 5, 100), (3, 5, 113), (3, 10, 100), (3, 10, 113), (3, 12, 100), (3, 12, 113)]

idx = ['c{}'.format(i) for i in range(1, len(data)+1)]
data = list(itertools.product(*a))
df = pd.DataFrame(data, index=idx, columns=list('abc')).T

df

    c1   c2   c3   c4   c5   c6   c7   c8   c9  c10  c11  c12
a    1    1    1    1    1    1    3    3    3    3    3    3
b    5    5   10   10   12   12    5    5   10   10   12   12
c  100  113  100  113  100  113  100  113  100  113  100  113

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...