I have the following simple data frame
import pandas as pd
df = pd.DataFrame({'column_a': ['a', 'b', 'c', 'd', 'e'],
                   'column_b': ['b', 'x', 'y', 'c', 'z']})
      column_a column_b
0        a        b
1        b        x
2        c        y
3        d        c
4        e        z
I'm looking to display the strings which occur in both columns:
result = ("b", "c")
Thanks
intersectionThis generalizes over any number of columns.
set.intersection(*map(set, map(df.get, df)))
{'b', 'c'}
                        Use  python's set object:
in_a = set(df.column_a)
in_b = set(df.column_b)
in_both = in_a.intersection(in_b)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With