1

I have a chunk of code that I received that only works with pandas dataframes as input. I currently have a pretty large numpy array. I need to convert this into a pandas dataframe.

The Dataframe will be 288 rows (289 counting the columns names) and 1801 columns. I have an array of size 1801 that will be all of the column names in the dataframe. Then I have an array of size (288) which will fill the first column. Then I have an array of shape (1800, 288) that will fill columns 2-1801. is there an easy way to turn this into a dataframe without individually defining all 1801 columns?

I know I could define columns like column2=array[0,:], column3=array[1,:] but that will be alot of work for 1801 columns.

1 Answer 1

5

You can pass a numpy array directly to the DataFrame constructor:

In [11]: a = np.random.rand(3, 5)

In [12]: a
Out[12]:
array([[ 0.46154984,  0.08813473,  0.57746049,  0.42924157,  0.34689139],
       [ 0.29731858,  0.83300176,  0.15884604,  0.44753895,  0.56840054],
       [ 0.02479636,  0.76544594,  0.24388046,  0.06679485,  0.94890838]])

In [13]: pd.DataFrame(a)
Out[13]:
          0         1         2         3         4
0  0.461550  0.088135  0.577460  0.429242  0.346891
1  0.297319  0.833002  0.158846  0.447539  0.568401
2  0.024796  0.765446  0.243880  0.066795  0.948908

In [14]: pd.DataFrame(a.T)
Out[14]:
          0         1         2
0  0.461550  0.297319  0.024796
1  0.088135  0.833002  0.765446
2  0.577460  0.158846  0.243880
3  0.429242  0.447539  0.066795
4  0.346891  0.568401  0.948908

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.