1

Is there a way to iterate across a dataframe where if a column name is equal to another column's value to input a '1' into that specific cell.

For example:

[A]  [B]  [C]  
D
E
B
C

would translate to:

[A] [B] [C]
D
E
B   1   
C       1

The null values in B and c would simply be NaN.

ParalysisByAnalysis
  • 703
  • 1
  • 4
  • 16

1 Answers1

1

This will do

df.fillna('').apply(lambda x : x.index==x.name).astype(int).replace(0,"")
Out[563]: 
      A  B  C
index        
D            
E            
B        1   
C           1
BENY
  • 317,841
  • 20
  • 164
  • 234