I have some data that looks like...
Year Make Model Trim
2007 Acura TL Base
2010 Dodge Avenger SXT
2009 Dodge Caliber SXT
2008 Dodge Caliber SXT
2008 Dodge Avenger SXT
Trim
has some missing values. What I would like to do is something like the following:
- Group by year make and model
- Impute Trim if there are missing valyes for that group
So for instance, I would look at all the 2007 Acura TL. That might look like
Year Make Model Trim
2007 Acura TL Base
2007 Acura TL XLR
2007 Acura TL NaN
2007 Acura TL Base
Then, I would impute the Nan with Base (since Base is the Mode). It is important to remember here that I want to do this for every group of Year, Make, and Model.
Use
groupby
thenmode
. Note thatmode
returns an array and you want to grab the first element of it. @John Galt deserves credit for this and gets my upvote.I use
assign
to create a copy ofdf
with an overwritten version of theTrim
column.You can overwrite the column directly with
Use mode
Use
inplace=True
to actually setIf you're working on groups