Home > Software design >  Group Dataframe rows while deleting specific columns
Group Dataframe rows while deleting specific columns

Time:05-06

I have a dataframe with several columns. I want to group rows based on multiple column values.

My source dataframe looks like this:

category   code  color    property_value        price
A          xx01  white    128                   $10.00
B          xx01  white    128                   $5.00
A          xx02  black    128                   $10.00
B          xx02  black    128                   $5.00
A          xx03  white    256                   $15.00
B          xx03  white    256                   $25.00

The purpose of the grouping is to delete columns color and code and only use property_value while saving categories.

target dataframe should look like :

category   property_value        price
A          128                   $10.00
B          128                   $5.00
A          256                   $15.00
B          256                   $25.00

Any leads on how I can achieve this result using pandas ?

CodePudding user response:

This seems more like a drop duplicate operation than a grouping operation:

# suppose your DataFrame is df
df = df[['category', 'property_value', 'price']].drop_duplicates(keep='first')
  • Related