python - Reindex data in an array such that missing data points are filled with NaNs -


i have several arrays 1 below:

[[ 0.          1.          0.73475787  0.36224658  0.08579446 -0.11767365   -0.09927562  0.17444341  0.47212111  1.00584593  1.69147789  1.89421069    1.4718292 ]  [ 2.          1.          0.68744907  0.38420843  0.25922927  0.04719614    0.00841919  0.21967246  0.22183329  0.28910002  0.54637077 -0.04389335   -1.33445338]  [ 3.          1.          0.77854922  0.41093192  0.0713814  -0.08194854   -0.07885753  0.1491798   0.56297583  1.0759857   1.57149366  1.37958867    0.64409152]  [ 5.          1.          0.09182989  0.14988215 -0.1272845   0.12154707   -0.01194815 -0.06136953  0.18783772  0.46631855  0.78850281  0.64755372    0.69757144]] 

please note, array[i,0] gives me count. in particular array counts 1, 4, , 6 missing. in other case might 2, 3, 5 or not missing.

now, later metaanalysis have arrays have nans included missing counts.

in above example, have

[[ 0.          1.          0.73475787  0.36224658  0.08579446 -0.11767365   -0.09927562  0.17444341  0.47212111  1.00584593  1.69147789  1.89421069    1.4718292 ] [[ 1.          nan          nan         nan        nan         nan    nan         nan          nan         nan        nan         nan    nan ]  [ 2.          1.          0.68744907  0.38420843  0.25922927  0.04719614    0.00841919  0.21967246  0.22183329  0.28910002  0.54637077 -0.04389335   -1.33445338]  [ 3.          1.          0.77854922  0.41093192  0.0713814  -0.08194854   -0.07885753  0.1491798   0.56297583  1.0759857   1.57149366  1.37958867    0.64409152] [[ 4.          nan          nan         nan        nan         nan    nan         nan          nan         nan        nan         nan    nan ]  [ 5.          1.          0.09182989  0.14988215 -0.1272845   0.12154707   -0.01194815 -0.06136953  0.18783772  0.46631855  0.78850281  0.64755372    0.69757144]] [[ 6.          nan          nan         nan        nan         nan    nan         nan          nan         nan        nan         nan    nan ] 

to re-sort arrays have tried following:

influence_incl_missing = np.ones((len(vec_conc),len(results)+1)) i, conc in enumerate(vec_conc):     if == influence[i,0]:         influence_incl_missing[i,:] = influence[i,:]     else:         influence_incl_missing[i,1:] = np.full(len(results),np.nan)         influence_incl_missing[i,0] = 

this gives me obvious error

indexerror: index 4 out of bounds axis 0 size 4 

since len(influence) < len(vec_conc).

how can in python?

many thanks!!

install pandas:

pip install pandas 

load data pandas dataframe , apply reindex operation – should it.

import pandas pd  df = pd.dataframe(arr)  # arr array  arr = df.set_index(df.columns[0])\         .reindex(range(len(vec_conc)))\         .reset_index().values 

arr  array([[ 0.        ,  1.        ,  0.73475787,  0.36224658,  0.08579446,         -0.11767365, -0.09927562,  0.17444341,  0.47212111,  1.00584593,          1.69147789,  1.89421069,  1.4718292 ],        [ 1.        ,         nan,         nan,         nan,         nan,                 nan,         nan,         nan,         nan,         nan,                 nan,         nan,         nan],        [ 2.        ,  1.        ,  0.68744907,  0.38420843,  0.25922927,          0.04719614,  0.00841919,  0.21967246,  0.22183329,  0.28910002,          0.54637077, -0.04389335, -1.33445338],        [ 3.        ,  1.        ,  0.77854922,  0.41093192,  0.0713814 ,         -0.08194854, -0.07885753,  0.1491798 ,  0.56297583,  1.0759857 ,          1.57149366,  1.37958867,  0.64409152],        [ 4.        ,         nan,         nan,         nan,         nan,                 nan,         nan,         nan,         nan,         nan,                 nan,         nan,         nan],        [ 5.        ,  1.        ,  0.09182989,  0.14988215, -0.1272845 ,          0.12154707, -0.01194815, -0.06136953,  0.18783772,  0.46631855,          0.78850281,  0.64755372,  0.69757144],        [ 6.        ,         nan,         nan,         nan,         nan,                 nan,         nan,         nan,         nan,         nan,                 nan,         nan,         nan]]) 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -