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
Use
GroupBy.transform
for count values per groups, compare bySeries.eq
for==
by original and last set1
by mask:Or: