0

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('')
2
  • 1
    Take a look at the numpy set_printoptions function. Commented Dec 20, 2022 at 15:20
  • The printoption for the original_data could look like this: np.set_printoptions(formatter={'all':lambda x: f'{x:.0f}'}) Commented Dec 20, 2022 at 15:47

1 Answer 1

2

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])
Sign up to request clarification or add additional context in comments.

Comments

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.