I have Dataframe looks like this:
F1 F2 F3 F4 F5
F1 1 1 1 1 1
F2 0 1 1 0 1
F3 0 1 1 1 1
F4 0 0 0 1 0
F5 0 1 1 1 1
Output I need:
HOR VER IS
F1 [F1, F2, F3, F4, F5] [F1] [F1]
F2 [F2, F3, F5] [F1, F2, F3, F5] [F2, F3, F5]
F3 [F2, F3, F4, F5] [F1, F2, F3, F5] [F2, F3, F5]
F4 [F4] [F1, F3, F4, F5] [F4]
F5 [F2, F3, F4, F5] [F1, F2, F3, F5] [F2,F3,F5]
In HOR: I want to show the list of column names containing 1 horizontally. In VER: I want to show the list of row names containing 1 vertically. IS is the intersection of HOR and VER
I don't know how to code this. Please help!
CodePudding user response:
Leverage boolean indexing and sets. Code below
s=df==1#Boolean values where 1
df = df.assign(HOR = s.agg(lambda s: s.index[s].values, axis=1),
VER = s.agg(lambda s: s.index[s].values, axis=0)
)
df['IS'] =list(df.apply(lambda x: list(set(x['HOR']).intersection(set(x['VER']))), axis=1))
F1 F2 F3 F4 F5 HOR VER IS
F1 1 1 1 1 1 [F1, F2, F3, F4, F5] [F1] [F1]
F2 0 1 1 0 1 [F2, F3, F5] [F1, F2, F3, F5] [F5, F3, F2]
F3 0 1 1 1 1 [F2, F3, F4, F5] [F1, F2, F3, F5] [F5, F3, F2]
F4 0 0 0 1 0 [F4] [F1, F3, F4, F5] [F4]
F5 0 1 1 1 1 [F2, F3, F4, F5] [F1, F2, F3, F5] [F5, F3, F2]