1

Maybe I could synthesize precisely my problem with my title, however I guess that by explaning it, things will get more clear.

So, what I want to do is the following: I want to create a dataframe which each row combinates each distinct element of 4 different numpy arrays.

I am trying to avoid for loops as much as possible and I am not sure if there are other means to achieve my goal utilizing pandas or python methods which I am not still aware of. The problem that I am trying to solve is naturally more complex and would involve several more arrays as well as more complex data.

I would really appreciate your help on this one!

min_x = 1

max_x = 5


x1_set = np.linspace(min_x, max_x, 5)

x2_set = np.linspace(min_x, max_x, 5)

x3_set = np.linspace(min_x, max_x, 5)

x4_set = np.linspace(min_x, max_x, 5)

X_set_df = pd.DataFrame([x1_set,x2_set,x3_set,x4_set]).T

I would expect a dataframe that would be somehow like this

First row : 1,1,1,1

Second row: 1,1,1,2

Third row: 1,1,1,3

...

n-row: 5,5,5,5

1 Answer 1

1

Use itertools.product for cartesian product, last pass it to DataFrame constructor:

from  itertools import product
df = pd.DataFrame(list(product(*[x1_set,x2_set,x3_set,x4_set])))
print (df)
       0    1    2    3
0    1.0  1.0  1.0  1.0
1    1.0  1.0  1.0  2.0
2    1.0  1.0  1.0  3.0
3    1.0  1.0  1.0  4.0
4    1.0  1.0  1.0  5.0
..   ...  ...  ...  ...
620  5.0  5.0  5.0  1.0
621  5.0  5.0  5.0  2.0
622  5.0  5.0  5.0  3.0
623  5.0  5.0  5.0  4.0
624  5.0  5.0  5.0  5.0

[625 rows x 4 columns]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.