mercredi 17 mars 2021

Pandas update column values based on certain conditions for a groupby object

I have a pandas data frame, where 3 columns X, Y, and Z are used for grouping. I want to update column B (or store it in a separate column) for each group based on the conditions shown in the code. But all I'm getting is nulls as the final outcome. I'm not sure what I'm doing incorrect.

Here's the table and code for the reference:

enter image description here

group=df.groupby(['X','Y','Z'])
for a,b in group:
    if ((b.colA==2).all()):
        df['colB']=b.colB.max() 
    elif (((b.colA>2).all()) and (b.colB.max() >=2)):
        df['colB']=b.colB.max()
    else:
        df['colB']=np.nan

Aucun commentaire:

Enregistrer un commentaire