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?

5
  • 1
    How does this work for you? I get TypeError: ufunc 'add' did not contain a loop with signature matching types. Commented Aug 18, 2018 at 23:13
  • @coldspeed : can you post the code you are trying to execute? Commented Aug 18, 2018 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. Commented Aug 18, 2018 at 23:26
  • Can't you treat '|' just like another string that you add to the others? Commented Aug 19, 2018 at 0:03
  • reduce(lambda x,y:np.add.outer(x,y),df.T.values.tolist()) Commented Aug 19, 2018 at 2:46

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.