abcdef
abcdef

Reputation: 115

How to change list of tuples to numpy array?

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) ]

Upvotes: 1

Views: 1058

Answers (1)

cs95
cs95

Reputation: 402363

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.

Upvotes: 3

Related Questions