0

I have a dataframe df1=

 HPE FRE UNE
0  S0  S0  S0
1  S1  S1  S1

using the below code("reduce" is a functool function, sta is a list containing the columns of df1):

reduce(lambda x,y:np.add.outer(x,y),sta).reshape(-1)

I reduced my dataframe to numpy array like this:

['S0S0S0' 'S0S0S1' 'S0S1S0' 'S0S1S1' 'S1S0S0' 'S1S0S1' 'S1S1S0' 'S1S1S1']

but I want my output to look like below:

['S0|S0|S0' 'S0|S0|S1' 'S0|S1|S0' 'S0|S1|S1' 'S1|S0|S0' 'S1|S0|S1' 'S1|S1|S0' 'S1|S1|S1']

How can I do?

Bhanu Tez
  • 296
  • 2
  • 14
  • 1
    How does this work for you? I get `TypeError: ufunc 'add' did not contain a loop with signature matching types`. – cs95 Aug 18 '18 at 23:13
  • @coldspeed : can you post the code you are trying to execute? – Bhanu Tez Aug 18 '18 at 23:20
  • This kind of `add` only works it the array is object dtype. Then the array `add` delegates the task to the `add` method of each element, which for strings is a `join`. It does not work for `string` dtype arrays. It's a feature, not a designed or documented behavior. – hpaulj Aug 18 '18 at 23:26
  • Can't you treat '|' just like another string that you `add` to the others? – hpaulj Aug 19 '18 at 00:03
  • `reduce(lambda x,y:np.add.outer(x,y),df.T.values.tolist())` – cs95 Aug 19 '18 at 02:46

1 Answers1

1

I'd do it this way:

pd.Series(map('|'.join, itertools.product(*sta)))

Assuming a less-degenerate input df1 to make the example more clear:

  HPE FRE UNE
0  AB  CD  EF
1  GH  IJ  KL

and sta = [df1.HPE, df1.FRE, df1.UNE], the result is:

0    AB|CD|EF
1    AB|CD|KL
2    AB|IJ|EF
3    AB|IJ|KL
4    GH|CD|EF
5    GH|CD|KL
6    GH|IJ|EF
7    GH|IJ|KL
John Zwinck
  • 239,568
  • 38
  • 324
  • 436