With data like this
import pandas as pd tcd = pd.DataFrame({ 'a': {'p_1': 1, 'p_2': 1, 'p_3': 0, 'p_4': 0}, 'b': {'p_1': 0, 'p_2': 1, 'p_3': 1, 'p_4': 1}, 'c': {'p_1': 0, 'p_2': 0, 'p_3': 1, 'p_4': 0}}) tcd
(but with columns 40e3)
I am looking for a vector way to put booleans and in the resulting row:
a & b = ab -> 1 or True a & c = ac -> 0 or False 1 0 0 1 0 0 1 1 0 1 0 0 0 1 1 0 1 0 0 1 0 0 0 0
At the moment I am only getting an ugly solution with a for :: loop
res = pd.Series(index=['a&a', 'a&b', 'a&c']) for i in range(3): res[i] = (tcd.iloc[:, 0] & tcd.iloc[:, i]).any() res aa 1 ab 1 ac 0
with BM answer I get it
def get_shared_p(tcd, i): res = (tcd.iloc[:, i][:, None] & tcd).any() res.index += '&_{}'.format(i) return res res = pd.DataFrame(columns=range(cols), index=range(cols)) for col_i in range(cols): res.iloc[:, col_i] = list(get_shared_p(tcd, col_i)) print res # 0 1 2 # 0 True True False # 1 True True True # 2 False True True
Perhaps we will avoid this new cycle.
source share