Question: I have the following data frame my\_df:

```
Name      cards
------------------
John      {A,B}
Mary      {B,C,A}
Dan       {D,A}
Peter     {C,A}
Ed        {A,C,D}

```

and I want to do a binary encoding for the set values, i.e. I want the output to be like:

```
Name     Card_A    Card_B    Card_C   Card_D
--------------------------------------------
John      1          1         0        0
Mary      1          1         1        0
Dan       1          0         0        1
Peter     1          0         1        0
Ed        1          0         1        1

```

Is there an existing python package for this? Or what would be the best way to achieve this? Thanks!

Answer: 
I would go with a reduce operation of some sort, something like this:

```
 int howMany = Stream.of("a", "vc", "ads", "ts", "ta").reduce(0, (i, string) -> {
        if (string.contains("a")) {
            // process a in any other way
            return i+1;
        }
        return i;
    }, (left, right) -> null); // override if parallel stream required

    System.out.println(howMany);

```
This can be done with `peek` function, as it returns a stream consisting of the elements of this stream, additionally performing the provided action on each element as elements are consumed from the resulting stream.

```
AtomicInteger counter = new AtomicInteger(0);

elements 
  .stream()
  .forEach(doSomething())
  .peek(elem -> counter.incrementAndGet());

int elementsProcessed = counter.get();

```