Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform an object into an array

I am learning python and I am having a little bit of trouble while handling an object. I have tried to look for a solution but it led nowhere, so I am asking you guys.

I want to get the first X columns of an object, but I can't as it doesn't have the same size in every row.

I have this object:

array([[45, 45, 45, 50, 51, 50, 50, 50, 51, 50, 52],
       [45, 45, 45, 50, 51, 50, 50, 50, 51, 50, 51, 52, 55],
       [45, 45, 45, 50, 51, 50, 52, 50, 50, 50, 51],
       [50, 51, 52, 55, 50, 52, 50, 50, 50, 51, 50, 51]], dtype=object)

And I would like to get something like this:

array([[45, 45, 45, 50],
       [45, 45, 45, 50],
       [45, 45, 45, 50],
       [50, 51, 52, 55]])

What could I do to solve this? Thanks for your help

Alvaro

like image 332
Alvaro Silva Avatar asked Apr 29 '26 08:04

Alvaro Silva


1 Answers

What about

import numpy as np
data = np.array([[45, 45, 45, 50, 51, 50, 50, 50, 51, 50, 52],
                 [45, 45, 45, 50, 51, 50, 50, 50, 51, 50, 51, 52, 55],
                 [45, 45, 45, 50, 51, 50, 52, 50, 50, 50, 51],
                 [50, 51, 52, 55, 50, 52, 50, 50, 50, 51, 50, 51]], dtype=object)
newData = np.array([d[:4] for d in data])
like image 105
Guillaume Jacquenot Avatar answered Apr 30 '26 20:04

Guillaume Jacquenot