I have a df
with multiple columns.
df = pd.DataFrame({'Store':['M1','M2','M3','M1','M1','M2','M2','M3','M3'],
'Category':['A','A','A','B','B','B','C','C','C'],
'Price_Category':[np.nan,X,np.nan,np.nan,Y,Y,Z,np.nan,Z]})
How do I fill the NaN
on Price_Category
with the mode, based only on Category
?
I tried to use:
df['Price_Category'] = df.groupby('Category')['Price_Category'].apply(lambda x: x.fillna(x.mode()[0]))
But I get this error: KeyError: 0
Other ways that I tried, started to fill the NaN
with the name from the Store
.
Thanks for your help!