python - numpy array to ndarray -


i have exported pandas dataframe numpy.array object.

subset = array[:4,:] array([[  2.        ,  12.        ,  33.33333333,   2.        ,          33.33333333,  12.        ],        [  2.        ,   2.        ,  33.33333333,   2.        ,          33.33333333,   2.        ],        [  2.8       ,   8.        ,  45.83333333,   2.75      ,          46.66666667,  13.        ],        [  3.11320755,  75.        ,  56.        ,   3.24      ,          52.83018868,  33.        ]]) print subset.dtype dtype('float64') 

i convert column values specific types, , set column names well, means need convert ndarray.

here dtypes:

[('percent_a_new', '<f8'), ('joinfield', '<i4'), ('null_count_b', '<f8'),  ('percent_comp_b', '<f8'), ('ranking_a', '<f8'), ('ranking_b', '<f8'), ('null_count_b', '<f8')] 

when go convert array, get:

 valueerror: new type not compatible array. 

how cast each column specific value can convert array ndarray?

thanks

you have ndarray. seeking structured array, 1 compound dtype. first see if pandas can you. if fails might able tolist , list comprehension.

in [84]: dt=[('percent_a_new', '<f8'), ('joinfield', '<i4'), ('null_count_b', '<     ...: f8'),      ...: ('percent_comp_b', '<f8'), ('ranking_a', '<f8'), ('ranking_b', '<f8'),     ...: ('null_count_b', '<f8')] in [85]: subset=np.array([[  2.        ,  12.        ,  33.33333333,   2.            ...:  ,     ...:          33.33333333,  12.        ],     ...:        [  2.        ,   2.        ,  33.33333333,   2.        ,     ...:          33.33333333,   2.        ],     ...:        [  2.8       ,   8.        ,  45.83333333,   2.75      ,     ...:          46.66666667,  13.        ],     ...:        [  3.11320755,  75.        ,  56.        ,   3.24      ,     ...:          52.83018868,  33.        ]]) in [86]: subset out[86]:  array([[  2.        ,  12.        ,  33.33333333,   2.        ,          33.33333333,  12.        ],        [  2.        ,   2.        ,  33.33333333,   2.        ,          33.33333333,   2.        ],        [  2.8       ,   8.        ,  45.83333333,   2.75      ,          46.66666667,  13.        ],        [  3.11320755,  75.        ,  56.        ,   3.24      ,          52.83018868,  33.        ]]) 

now make array dt. input structured array has list of tuples - i'm using tolist , list comprehension

in [87]: np.array([tuple(row) row in subset.tolist()],dtype=dt) .... valueerror: field 'null_count_b' occurs more once in [88]: subset.shape out[88]: (4, 6) in [89]: dt out[89]:  [('percent_a_new', '<f8'),  ('joinfield', '<i4'),  ('null_count_b', '<f8'),  ('percent_comp_b', '<f8'),  ('ranking_a', '<f8'),  ('ranking_b', '<f8'),  ('null_count_b', '<f8')] in [90]: dt=[('percent_a_new', '<f8'), ('joinfield', '<i4'), ('null_count_b', '<     ...: f8'),      ...: ('percent_comp_b', '<f8'), ('ranking_a', '<f8'), ('ranking_b', '<f8')] in [91]: np.array([tuple(row) row in subset.tolist()],dtype=dt) out[91]:  array([(2.0, 12, 33.33333333, 2.0, 33.33333333, 12.0),        (2.0, 2, 33.33333333, 2.0, 33.33333333, 2.0),        (2.8, 8, 45.83333333, 2.75, 46.66666667, 13.0),        (3.11320755, 75, 56.0, 3.24, 52.83018868, 33.0)],        dtype=[('percent_a_new', '<f8'), ('joinfield', '<i4'), ('null_count_b', '<f8'), ('percent_comp_b', '<f8'), ('ranking_a', '<f8'), ('ranking_b', '<f8')]) 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -