Code Breakdown For Better Understanding

Screen Link:

My Code:

def clean(row):
num_death = 0
cols = [‘Death1’, ‘Death2’, ‘Death3’, ‘Death4’, ‘Death5’]

for c in cols:
    death = row[c]
    if pd.isnull(death) or death == "NO":
        continue
    elif death == "YES":
        num_death += 1
return num_death

true_avengers[“Deaths”] = true_avengers.apply(clean, axis=1)

true_avengers

Replace this line with your code


What I expected to happen:


What actually happened: 

Replace this line with the output/error


<!--Enter other details below: -->

I was just wondering what does the row variable stand for?
Is it the dataframe?
A breakdown of the code would be very helpful. I just want to get a better understanding.

Hi @chautran0729,

Yes, the variable row is supposed to be a row of the dataframe with which we’re working here. We can apply our created function clean() to the dataframe using the method apply(), then we add the argument axis=1, which exactly means across columns, or, in other words, along rows. That is, we’re applying this function to each row of the dataframe.