Pandas fillna() based on specific column attribute
问题 Let's say I have this table Type | Killed | Survived Dog 5 2 Dog 3 4 Cat 1 7 Dog nan 3 cow nan 2 One of the value on Killed is missing for [Type] = Dog . I want to impute the mean in [Killed] for [Type] = Dog . My code is as follow: Search for the mean df[df['Type'] == 'Dog'].mean().round() This will give me the mean (around 2.25) Impute the mean (This is where the problem begins) df.loc[(df['Type'] == 'Dog') & (df['Killed'])].fillna(2.25, inplace = True) The code runs, but the value is not