Steven G Steven G - 3 months ago 20
Python Question

create a pandas frame of shape (x**0.5,x**0.5) from a array of size x python

I have an array of size 64, such has:

In[164]: x_y.values / x_ysquare.stack().values
Out[164]:
array([ 1. , 0.01623716, -0.03305102, 0.03264311, -0.0175754 ,
0.04017079, 0.15731795, -0.01797369, 0.01623716, 1. ,
0.08387368, -0.09562322, 0.02700502, 0.0614588 , 0.03461564,
-0.12421004, -0.03305102, 0.08387368, 1. , -0.00248859,
-0.00391474, 0.01603743, 0.05942098, 0.08989135, 0.03264311,
-0.09562322, -0.00248859, 1. , -0.16354249, -0.00887474,
0.30343543, 0.12873483, -0.0175754 , 0.02700502, -0.00391474,
-0.16354249, 1. , 0.02347214, -0.30337839, -0.09302462,
0.04017079, 0.0614588 , 0.01603743, -0.00887474, 0.02347214,
1. , -0.01125003, -0.31859215, 0.15731795, 0.03461564,
0.05942098, 0.30343543, -0.30337839, -0.01125003, 1. ,
0.18483076, -0.01797369, -0.12421004, 0.08989135, 0.12873483,
-0.09302462, -0.31859215, 0.18483076, 1. ])


I am trying to create a pandas dataframe of size 8x8 from those 64 data.

this is a correlation matrix where I am forcing the mean to be 0, I am trying to get the exact same output shape as if I were using df.corr() method.

thanks for the help!

Answer

Maybe you can do:

arr_reshaped = arr.reshape(8,8)

df = pd.DataFrame(arr_reshaped)