3

I have a dataframe similar to this (but much larger).

>>> df = pd.DataFrame([ [ 'a', np.array([ 1, 2]) ], [ 'b', np.array([ 3, 4 ]) ] ])
   0       1
0  a  [1, 2]
1  b  [3, 4]

The last column has the shape listed as...

>>> df[1].shape
(2,)

I'd like it to be listed as (2,2). I was able to do this via the following line, but the performance of tolist() is... bad.

>>> np.array(df[1].tolist()).shape
(2, 2)

It could also be a Pandas dataframe as long as it correctly reports the shape. Any other suggestions?

2
  • What are you trying to accomplish by having changing the shape to (2,2)? Will df[1] always be an array of 2 numbers? Commented Nov 8, 2018 at 14:38
  • @vealkind yes, it is. Commented Nov 8, 2018 at 15:03

1 Answer 1

7

This is not possible!

Pandas keeps each Series as a single dimensional ndarray. If you have multiple dimensions that you are trying to squeeze into it, Pandas will force this to be a single dimensional array with dtype of object.

If you simply want to get the contents and make it into a 2 dimensional array then I'd suggest

np.array(df[1].values.tolist())

Otherwise, I'd suggest you keep them in two different columns.

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

10 Comments

It doesn't need to be a Pandas series. It could be a numpy array. I'm wondering if there's better way than converting it to a list first.
The difference between what I've done and what you did was that I accessed the values attribute and used Numpy's tolist rather than going through Pandas' tolist. But it's the best option you have. If you are worried about performance, then you should reconsider putting 2-element array like things in a single column in the first place.
It's actually (50, 25). So not quite the same as just a 2-element array. That was a toy problem.
Same thing. Keep in a larger dataframe with appropriate number of columns.
I do need to work with it in a (50, 25) format. Do you think reshaping is faster than the conversion?
|

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.