Tuesday 15 March 2011

numpy - The python version of R's %in% function -


I have a 1D integer array of "factor" which means that there are different things. Sometimes, many numbers mean the same thing:

  import such as np vec = np.arange (1, 10) comps = {'good': (3,), ' Bad ': (4, 5, 9,),' OK ': (2, 3,)} result = {} for name in comps.keys (): Results [name] = np.zeros (len (vec) , 'Bool') Calculation (vec) in I, v: [name] [i] = comps in v [name]   

This is the desired output though vec gets bigger and the number of keys increases in comps , it becomes very slow plus, its yucky ... r in %% % is the function:

  vec = 1:10 comp = list = 3, bad = C (4: 5, 9), OK = 2: 3) Lapli (comp, function (x) vec% x)   

Comparison between values ​​and gives the same length as the "code" vec in the form of "logical" or "boolean vector".

I can get close and pandas :

  PDDF = PD Import the dataframe ({'vec': vec}) to use pandas in the result = {} comps.keys for the name (): the result [name] = df.vec.apply (lambda x: x Comps [name])   

For the same type, but I want elementwise array instead of a boolean result.

What is the best way to do this in dragon? (Daddy Penda?)

You can create a dictionary by understanding it (and series method) :

  for pd.DataFrame ({k: df.vec.isin (v) k, comps.iteritems ()} in v.    

No comments:

Post a Comment