Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to mix data types you are going to want <a href="http://docs.scipy.org/doc/numpy/user/basics.rec.html" rel="nofollow">structured arrays</a>.</p> <p>If you are going to want the index of matching values in a lookup array you want <code>searchsorted</code></p> <p>Your example goes like this:</p> <pre><code>&gt;&gt;&gt; import numpy as np &gt;&gt;&gt; PS = np.array([ ('A', 'LABEL1', 20), ('B', 'LABEL2', 15), ('C', 'LABEL3', 120), ('D', 'LABEL1', 3),], dtype=('a1,a6,i4')) &gt;&gt;&gt; FACTOR = np.array([ ('LABEL1', 0.1), ('LABEL2', 0.5), ('LABEL3', 10)],dtype=('a6,f4')) </code></pre> <p>Your structured arrays:</p> <pre><code>&gt;&gt;&gt; PS array([('A', 'LABEL1', 20), ('B', 'LABEL2', 15), ('C', 'LABEL3', 120), ('D', 'LABEL1', 3)], dtype=[('f0', '|S1'), ('f1', '|S6'), ('f2', '&lt;i4')]) &gt;&gt;&gt; FACTOR array([('LABEL1', 0.10000000149011612), ('LABEL2', 0.5), ('LABEL3', 10.0)], dtype=[('f0', '|S6'), ('f1', '&lt;f4')]) </code></pre> <p>And you can access individual fields like this (or you can give them names; see the docs):</p> <pre><code>&gt;&gt;&gt; FACTOR['f0'] array(['LABEL1', 'LABEL2', 'LABEL3'], dtype='|S6') </code></pre> <p>How to perform the lookup of FACTOR on PS (FACTOR must be sorted):</p> <pre><code>&gt;&gt;&gt; idx = np.searchsorted(FACTOR['f0'], PS['f1']) &gt;&gt;&gt; idx array([0, 1, 2, 0]) &gt;&gt;&gt; FACTOR['f1'][idx] array([ 0.1, 0.5, 10. , 0.1], dtype=float32) </code></pre> <p>Now simply create a new array and multiply:</p> <pre><code>&gt;&gt;&gt; newp = PS.copy() &gt;&gt;&gt; newp['f2'] *= FACTOR['f1'][idx] &gt;&gt;&gt; newp array([('A', 'LABEL1', 2), ('B', 'LABEL2', 7), ('C', 'LABEL3', 1200), ('D', 'LABEL1', 0)], dtype=[('f0', '|S1'), ('f1', '|S6'), ('f2', '&lt;i4')]) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload