I have a dataset in pandas (say two class).
index | length | weight | label
-------|--------|--------|-------
0 1 2 0
1 2 3 0
2 nan 4 0
3 6 nan 0
4 30 40 1
5 45 35 1
6 18 nan 1
df.fillna(df.mean()) returns a dataframe which each nan is filled by mean of each column. But I want to fill each nan in each column with mean of its class so length at index 2 would be 3. Output is like this:
index | length | weight | label
-------|--------|--------|-------
0 1 2 0
1 2 3 0
2 3 4 0
3 6 3 0
4 30 40 1
5 45 35 1
6 18 37.5 1
Is there a simple function or I should implement it myself?
Use
GroupBy.transformwithmeanfor helperDataframewith means per groups and pass tofillna:Detail: