I have this dataframe, How can i make condition that if i have a duplicate row if that they are exactly the same(Mercedes exp) I keep only one (without making the sum) Or make the sum (kia case) if there is a diffrence in rent/sale value
Df example
cars rent sale
Kia 1 2
Bmw 1 4
Mercedes 2 1
Ford 1 1
Kia 4 5
Mercedes 2 1
i write this code:
import pandas as pd
df=pd.DataFrame({'cars':['Kia','Bmw','Mercedes','Ford','Kia','Mercedes'],
'rent':[1,1,2,1,4,2],
'sale':[2,4,1,1,5,1]})
df=df.groupby(['cars']).sum().reset_index()
print(df)
I got this output:
cars rent sale
0 Bmw 1 4
1 Ford 1 1
2 Kia 5 7
3 Mercedes 4 2
Expected output:
cars rent sale
0 Kia 5 7
1 Bmw 1 4
2 Mercedes 2 1
3 Ford 1 1