On the last post i posted that i have a problem to remove/sum row based on certains conditions and someone helped to right a code like this.
Here is my code:
import pandas as pd
df=pd.DataFrame({
'cars':['Kia rio','Bmw','Mercedes','Ford','Kia','Mercedes Benz'],
'rent':[1,1,2,1,4,2],
'sale':[2,4,1,1,5,1],
'id':[2000,1000,3000,4000,2000,3000]
})
print(df)
df1 = df.drop_duplicates().groupby(['id'], sort=False, as_index=False).sum()
print(df1)
But when I run the groupby method it drop car columns. Can anyone help me on this?
I got this output:
id rent sale
0 2000 5 7
1 1000 1 4
2 3000 4 2
3 4000 1 1
Expected output:
cars rent sale id
Kia 5 7 2000
Bmw 1 4 1000
Mercedes 2 1 3000
Ford 1 1 4000