1

I am trying to join recarrys in python such that the same value joins to many elements. The following code works when it is a 1:1 ratio, but when I am trying to do many:1, it only joins one instance:

import numpy as np
import matplotlib
# First data structure
sex = np.array(['M', 'F', 'M', 'F', 'M', 'F'])
causes = np.array(['c1', 'c1', 'c2', 'c2', 'c3', 'c3'])
data1 = np.core.records.fromarrays([sex, causes], names='sex, causes')

# Second data structure
causes2 = np.array(['c1', 'c2', 'c3'])
analyst = np.array(['p1', 'p2', 'p3'])
data2 = np.core.records.fromarrays([causes2, analyst], names='causes, analyst')

# Join on Cause
all_data = matplotlib.mlab.rec_join(('causes'), data1, data2, jointype='leftouter')

What I would like the all_data recarray to contain is all of the data from data1 with the corresponding analyst indicated in data2.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
mike
  • 22,931
  • 31
  • 77
  • 100
  • Anyone get anywhere with this not using matplotlib? Why is matplotlib handling the "join" function??? I guess I will post this as a separate question. – mathtick Apr 25 '12 at 17:48

1 Answers1

1

There might be a good use of record array, but I thought python dict should be as good here... Want to know numpy way of doing this myself, too, if it is good.

dct = dict(zip(data2['causes'], data2['analyst']))
all_data = mlab.rec_append_fields(data1, 'analyst',
    [dct[x] for x in data1['causes']])
yosukesabai
  • 6,184
  • 4
  • 30
  • 42