8

I've created some np.arrays to do some calculation with them. (All have the same size [100,1]) Now I want to create a pandas Dataframe and each array shoud be one column of that DF. The Names of the arrays should be the header of the DataFrame.

In Matlab I would easily do it like that:

Table = table(array1, array2, array3, ... );

How can I do this in Python?

thanks in advance!

1
  • Ah sorry I misread your question. You would need to reshape the arrays to use the syntax I provided. Commented Jul 30, 2017 at 13:12

2 Answers 2

9

Let's say these are your arrays:

arr1, arr2, arr3 = np.zeros((3, 100, 1))

arr1.shape
Out: (100, 1)

You can use hstack to stack them and pass the resulting 2D array to the DataFrame constructor:

df = pd.DataFrame(np.hstack((arr1, arr2, arr3)))

df.head()
Out: 
     0    1    2
0  0.0  0.0  0.0
1  0.0  0.0  0.0
2  0.0  0.0  0.0
3  0.0  0.0  0.0
4  0.0  0.0  0.0

Or name the columns as arr1, arr2, ...:

df = pd.DataFrame(np.hstack((arr1, arr2, arr3)), 
                  columns=['arr{}'.format(i+1) for i in range(3)])

which gives

df.head()
Out: 
   arr1  arr2  arr3
0   0.0   0.0   0.0
1   0.0   0.0   0.0
2   0.0   0.0   0.0
3   0.0   0.0   0.0
4   0.0   0.0   0.0
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that worked well! Is it possible that the header gets the arrays names instead of 0,1,2 ?
In Python, names of the objects are just references. An object may have multiple names. Because of that there is no easy way of getting the name of a variable, say arr1. You can iterate over the names in globals() but it is not recommended. Do your variable names have a structure? If so, it is easy to build the names instead of extracting them. (Like arr1, arr2, arr3...)
Thank you. No they are real names. So i think i really need to rename them in the Dataframe then.
2

Solution with numpy.concatenate for 2d array and DataFrame constructor:

df = pd.DataFrame(np.concatenate([arr1, arr2, arr3], axis=1), columns= ['a','b','c'])

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.