0

How to a convert a pandas column of numpy arrays to lists?

Pandas dataframe construction

A = np.random.randint(0,15000000,65000)                                      
B = [np.random.randint(0,15000000,k) for k in np.random.randint(2,101,100)]

A32 = A.astype(np.int32)

from itertools import chain

sizes = np.fromiter(chain((0,),map(len,B)),np.int32,len(B)+1)
boundaries = sizes.cumsum()

# force int32
B_all = np.empty(boundaries[-1],np.int32)
B32 = np.split(B_all, boundaries[1:-1])
df = pd.DataFrame([A32, B32]).T

Attempt to convert 2nd column to list

df[1] = df[1].apply(lambda x: x.tolist() )

Resulting error message

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-35-6d36b7b9c250> in <module>()
----> 1 df[1] = df[1].apply(lambda x: x.tolist() )

1 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/series.py in apply(self, func, convert_dtype, args, **kwds)
   3589             else:
   3590                 values = self.astype(object).values
-> 3591                 mapped = lib.map_infer(values, f, convert=convert_dtype)
   3592 
   3593         if len(mapped) and isinstance(mapped[0], Series):

pandas/_libs/lib.pyx in pandas._libs.lib.map_infer()

<ipython-input-35-6d36b7b9c250> in <lambda>(x)
----> 1 df[1] = df[1].apply(lambda x: x.tolist() )

AttributeError: 'float' object has no attribute 'tolist'
1

1 Answer 1

1

Firstly, I am going to assume A32 is a typo (since it doesn't exist), and that you meant to use A.

A has a length of 65000, B32 has a length of 100. Column 1 of your dataframe is getting extended to 65000 with NaNs (a float value), which obviously has no tolist method. That is why you are getting this error. To fix this, you can either use only the first 100 elements of A, that is A[:100], or you can just make B32 as long as A.

Sign up to request clarification or add additional context in comments.

1 Comment

I forgot a line where I convert it to int32, I added it in.

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.