mardi 19 octobre 2021

How to change diagonal values when row sum is a certain number

I believe this is a simple question for people with some level of experience in Python. I have a dataframe like this. What I want is that, if the row sums to 0, the diagonal value changes to 1. E.g. df.loc[2,2] and df.loc[3,3] become 1.

enter image description here

I had the code like this, but I know the if line must have several issues.

for i, j in zip(range(len(df.index)), range(len(df.columns))):
    if i == j & df.iloc[[i]].sum(axis = 1) == 0:
        df.loc[i, j] = 1

Aucun commentaire:

Enregistrer un commentaire