Change the value of column based on quantity of equals rows

91 views Asked by At

I have a dataframe like this:

df = pd.DataFrame({'id': ['B668441DE83B', 'B668441DE83B', 'B668441DE83B', '89C26DEE41E2', '89C26DEE41E2'],
                   'desc': ['Car', 'Car', 'Bus', 'Bus', 'Bus'],
                   'quantity': [2, 2, 1, 3, 3]})
print(df, '\n')
             id desc  quantity
0  B668441DE83B  Car         2
1  B668441DE83B  Car         2
2  B668441DE83B  Bus         1
3  89C26DEE41E2  Bus         3
4  89C26DEE41E2  Bus         3 

I need to change the value of quantity column to 1 if quantity value of row equals the actual quantity of rows, where columns id and desc are equals (row0 and row1 in this example).

Desired output:

             id desc  quantity
0  B668441DE83B  Car         1
1  B668441DE83B  Car         1
2  B668441DE83B  Bus         1
3  89C26DEE41E2  Bus         3
4  89C26DEE41E2  Bus         3 
1

There are 1 answers

0
jezrael On BEST ANSWER

Use GroupBy.transform for count values per groups, compare by Series.eq for == by original and last set 1 by mask:

mask = df.groupby(['id','desc'])['id'].transform('size').eq(df['quantity'])

df.loc[mask, 'quantity'] = 1

Or:

df['quantity'] = df['quantity'].mask(mask, 1)

print (df)
             id desc  quantity
0  B668441DE83B  Car         1
1  B668441DE83B  Car         1
2  B668441DE83B  Bus         1
3  89C26DEE41E2  Bus         3
4  89C26DEE41E2  Bus         3