1

I have a data frame as below:

  id      points
0  1      (-2.3, 7)
1  1      (-5, 7)
2  1      (-6.9, 5)
3  2      (2, 5.9)
4  2      (-0.3, -8)

I am trying to use groupby id and get a numpy 2darray like below:

df2 = df.groupby(["id"])["points"]\
          .apply(lambda x : np.array(x.values)).reset_index()

This works but it changes to list of tuples (like below), how to change to numpy array ? OR what I am considering as list of tuples is actually a numpy 2d array?

  id   points
0  1   [ (-2.3, 7), (-5,7), (-6.9,5) ]
1  2   [ (2, 5.9), (-0.3, -8) ]
3
  • A terrific answer here may help you: stackoverflow.com/questions/28176949/… Commented Aug 24, 2017 at 0:15
  • Actually, those are arrays they just look like lists when printed inside a pd.DataFrame Commented Aug 24, 2017 at 0:16
  • My answer assumes you wanted a 2D numpy array instead of a numpy array of tuples. Commented Aug 24, 2017 at 0:20

1 Answer 1

3

If what you want is a numpy array of tuples, then that's what you've already got:

In [8]: df.groupby('id').points.apply(np.asarray).values
Out[8]: 
array([array([(-2.3, 7), (-5, 7), (-6.9, 5)], dtype=object),
       array([(2, 5.9), (-0.3, -8)], dtype=object)], dtype=object)

However, if you want to convert your output to a 2D array instead of an array of tuples, read on.


Option 1

Convert points before groupby (you can manage without a lambda):

In [785]: df.points = df.points.apply(np.array); df
Out[785]: 
   id        points
0   1   [-2.3, 7.0]
1   1       [-5, 7]
2   1   [-6.9, 5.0]
3   2    [2.0, 5.9]
4   2  [-0.3, -8.0]

In [787]: df.groupby('id').points.apply(np.asarray)
Out[787]: 
id
1    [[-2.3, 7.0], [-5, 7], [-6.9, 5.0]]
2             [[2.0, 5.9], [-0.3, -8.0]]

Option 2

Convert points after groupby (you'll need a lambda for this):

In [796]: df.groupby('id').points.apply(lambda x: np.array(list(map(list, x))))
Out[796]: 
id
1    [[-2.3, 7.0], [-5.0, 7.0], [-6.9, 5.0]]
2                 [[2.0, 5.9], [-0.3, -8.0]]

Once done, call df.reset_index to get your desired output.

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

2 Comments

@juanpa.arrivillaga Ahahaha yes. The last time I restarted was when the prompt read 2k something. Will do.
@piRSquared Seems familiar? :p Haha, I think you know where I picked it up from.

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.