I have a data frame df `
df=pd.DataFrame([['1001',34.3],['1009',34.3],['1003',776],['1015',18.95],['1023',18.95],['1007',18.95],['1009',18.95],['1037',321.2],['1001',344.2],['1016',3.2],['1017',3.2],['1027',344.2]],columns=['id','amount'])
id amount
0 1001 34.30
1 1009 34.30
2 1003 776.00
3 1015 18.95
4 1023 18.95
5 1007 18.95
6 1009 18.95
7 1037 321.20
8 1001 344.20
9 1016 3.20
10 1017 3.20
11 1027 344.20
`
I would likw to have df_new grouped by consecutively repeating values in column 'amount' by first value:
`
id amount
0 1001 34.30
2 1003 776.00
3 1015 18.95
7 1037 321.20
8 1001 344.20
9 1016 3.20
11 1027 344.20
`
CodePudding user response:
here is one way to do it
# take a difference b/w the amount of two consecutive rows and then
# choose rows where the difference is not zero
out= df[df['amount'].diff().ne(0) ]
out
id amount
0 1001 34.30
2 1003 776.00
3 1015 18.95
7 1037 321.20
8 1001 344.20
9 1016 3.20
11 1027 344.20