jeudi 22 novembre 2018

if nan python pandas loop

This is a bit weird and I don't have a sample data frame but if anyone can help that would be great.

I have 3 columns A, B and C.

C might be blank / nan.

I was to say, if C is blank and A and B equal the same values as A and B of the row above. Then set C to the same value as C of the row above.

this is what I have so far. Its running but not chaning the values of C.

for i, row in df.iterrows():

    if df['C'][i]==np.nan:
        if df[['A','B']][i]==df[['A','B']][i-1]:
            df['C'][i]=df['C'][i-1]
        else:
            pass
    else:
        pass

Does anyone see why this might not be working?

Many thanks

Aucun commentaire:

Enregistrer un commentaire