Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reshape a pandas series after converting to numpy?

I have a pandas series like this:

import pandas as pd
import numpy as np

testpd = pd.Series([[[32, 43], [453, 565]], [[32, 43], [453, 565]]])

the shape of it is (2,)

I convert this into numpy array like the following

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

testnp = testpd.to_numpy()

the shape of the numpy array is (2,)

but ideally, it should be (2, 2, 2) or whatever the actual dimensions are, like the shape of testnps here is (2,2,2)

testnps = np.array([[[32, 43], [453, 565]], [[32, 43], [453, 565]]])

I tried to do testnp.reshape(2,2) but it gave an error "ValueError: cannot reshape array of size 2 into shape (2,2)".

How can I reshape the array to get the ideal (2,2,2) shape?

>Solution :

First you can convert the pandas series into a list then cover that into numpy array. You can do

testnp = np.array(testpd.tolist())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading