stack offer
stack offer

Reputation: 155

How to delete space in numpy array

inside the array data there will be space, how to delete space (' ') in numpy array.

Original data

array([  0.        ,  14.        ,  52.        ,  65.        ,
        14.        ,   2.        ,   0.        ,   0.        ,
         0.        ,   0.        ,  19.        , 100.        ,
        82.        ,   2.        ,  46.        ,   0.        ,
         0.        ,   0.        ,   0.        ,   0.        ,
        33.        ,  64.        ,  80.        ,  68.        ,
        51.        ,   0.        ,   0.        ,   0.        ,
         0.        ,  45.        ,  89.        ,  75.        ,
        48.        ,  35.        ,   0.        ,   0.        ,
         0.        ,   0.        ,   0.        ,  33.        ,
        90.50490629, 101.1683021 ,  76.        ,  67.8316979 ,
        21.62414551,  37.66339581,   0.        ,   0.        ,
        68.49509371, 129.8316979 , 134.8316979 , 106.        ,
         1.        ,   0.        ,   0.        ,   0.        ,
         0.        ,   6.        ,  62.        ,  46.        ,
        38.        ,  65.        ,  59.        ,  84.        ,
        66.        ,  94.        , 121.        , 140.        ,
       147.        , 143.        ,   0.        ,   0.        ,
         0.        ,   0.        ,  79.        , 113.        ,
       130.        , 148.        , 152.        ,  72.        ,
         0.        ,   0.        ,   0.        , 132.        ,
       131.        ,  19.        ,   0.        ,   0.        ,
         0.        ])

I want to make data like this

array([0,14,52,65,14,0,0,0,0,0,19,100,82,0,46,0,0,0,0,0,33,64,80,68,51,0,0,0,0,45,89,75,48,35,0,0,0,0,0,33,93,102,76,67,0,36,0,0,66,129,134,106,0,0,0,0,0,6,62,46,38,65,59,84,66,94,121,140,147,143,0,0,0,0,79,113,130,148,152,72,0,0,0,132,131,19,0,0,0] )

I've try this method but that is no

SIData =  np.asarray(new_data).str.lstrip(' ').str.rstrip('')

Upvotes: 0

Views: 76

Answers (1)

ScottC
ScottC

Reputation: 4105

You could use round, and then convert to int using astype():

SIData = np.round(original_data, 0).astype(int)

Output:

array([  0,  14,  52,  65,  14,   2,   0,   0,   0,   0,  19, 100,  82,
         2,  46,   0,   0,   0,   0,   0,  33,  64,  80,  68,  51,   0,
         0,   0,   0,  45,  89,  75,  48,  35,   0,   0,   0,   0,   0,
        33,  91, 101,  76,  68,  22,  38,   0,   0,  68, 130, 135, 106,
         1,   0,   0,   0,   0,   6,  62,  46,  38,  65,  59,  84,  66,
        94, 121, 140, 147, 143,   0,   0,   0,   0,  79, 113, 130, 148,
       152,  72,   0,   0,   0, 132, 131,  19,   0,   0,   0])


Note:

If you convert to int directly, it will round down:

SIData = original_data.astype(int)

Output:

array([  0,  14,  52,  65,  14,   2,   0,   0,   0,   0,  19, 100,  82,
         2,  46,   0,   0,   0,   0,   0,  33,  64,  80,  68,  51,   0,
         0,   0,   0,  45,  89,  75,  48,  35,   0,   0,   0,   0,   0,
        33,  90, 101,  76,  67,  21,  37,   0,   0,  68, 129, 134, 106,
         1,   0,   0,   0,   0,   6,  62,  46,  38,  65,  59,  84,  66,
        94, 121, 140, 147, 143,   0,   0,   0,   0,  79, 113, 130, 148,
       152,  72,   0,   0,   0, 132, 131,  19,   0,   0,   0])

Upvotes: 2

Related Questions