'How to make a code for all rows in a pandas Dataframe using a code for one specific row?

I have a code for one row only row0 where I am applying a systematic procedure as follows:

import pandas as pd

z = [[1,1,0,0,1],
     [0,1,0,0,1],
     [0,1,1,1,1],
     [0,0,0,1,0],
     [0,0,1,0,1]]
z = pd.DataFrame(z)

df = z.copy()
df.index = ['F1','F2','F3','F4','F5']
df.columns = ['F1','F2','F3','F4','F5']
df
   F1  F2  F3  F4  F5
F1  1   1   0   0   1
F2  0   1   0   0   1
F3  0   1   1   1   1
F4  0   0   0   1   0
F5  0   0   1   0   1

a) Let's consider row 'F1'. Check the position of 0s in this row. For example, there are 0s at column 'F3','F4'.

row0 = z.loc[[0],:]
col_zeros = row0.loc[:, (row0 == 0).all(axis=0)]

b) Then I converted this column positions into a list

col_list = col_zeros.columns.to_list()

c) Using col_list to hide rows of same position labels 'F3','F4' from original dataframe z

df2 = z.copy()
df2.drop(df2.index[col_list], axis = 0, inplace = True)

d) Now checking the remaining columns one-by-one if there is 1 present in any of these columns

df3 = df2[col_list]

e) After checking, I can see 1 present in column 2 and not present in column 3. Hence Replacing 0 with 1 in row0 at column position 2 i.e., 'F3'

df.iloc[0,2] = 1

f) The entire code mentioned above from a) to e) is just for row 'F1'. I want the same procedure to be repeated for rest of the rows 'F2','F3','F4','F5'.

Output I need after executing above method for all df rows :

     F1  F2  F3  F4  F5
  F1  1   1   1   0   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


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source