0

Is it possible to create a dataframe from few 1d arrays and place them as columns? If I create a dataframe from 1 1d array everything is ok:

arr1 = np.array([11, 12, 13, 14, 15])
arr1_arr2_df = pd.DataFrame(data=arr1, index=None, columns=None) 
arr1_arr2_df
Out: 
    0
0  11
1  12
2  13
3  14
4  15

But If make a datafreme form 2 arrays they are placed is rows:

arr1 = np.array([11, 12, 13, 14, 15])
arr2 = np.array([21, 22, 23, 24, 25])
arr1_arr2_df = pd.DataFrame(data=(arr1,arr2), index=None, columns=None)
arr1_arr2_df
Out: 
    0   1   2   3   4
0  11  12  13  14  15
1  21  22  23  24  25

I know that I can achieve it by using transpose:

arr1_arr2_df = arr1_arr2_df.transpose()
arr1_arr2_df
Out: 
    0   1
0  11  21
1  12  22
2  13  23
3  14  24
4  15  25

But is it possible to get it from the start?

2
  • 3
    what about zip? arr1_arr2_df = pd.DataFrame(data=(zip(arr1,arr2)), index=None, columns=None) Commented Jun 2, 2020 at 16:44
  • 3
    since we r in the mood : pd.DataFrame(dict(enumerate((arr1,arr2)))) . But really, zip does the job succinctly Commented Jun 2, 2020 at 16:54

1 Answer 1

3

You can use a dictionary:

arr1_arr2_df = pd.DataFrame(data={0:arr1,1:arr2})
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.