Given the following data frame:
import pandas as pd
import numpy as np
df = pd.DataFrame({'Site':['A','A','A','B','B','B','C','C','C'],
'Value':[np.nan,1,np.nan,np.nan,2,2,3,np.nan,3]})
df
Site Value
0 A NaN
1 A 1.0
2 A NaN
3 B NaN
4 B 2.0
5 B 2.0
6 C 3.0
7 C NaN
8 C 3.0
I would like to fill in the NaN values with the most common (median or average) for the site. Desired Result:
Site Value
0 A 1.0
1 A 1.0
2 A 1.0
3 B 2.0
4 B 2.0
5 B 2.0
6 C 3.0
7 C 3.0
8 C 3.0
Thanks in advance!
Update: this is close, but there is no cigar:
df['Value']=df.groupby(['Site'])['Value'].fillna(min)
as a result...
Site Value
0 A <function amax at 0x108cf9048>
1 A 1
2 A <function amax at 0x108cf9048>
3 B <function amax at 0x108cf9048>
4 B 2
5 B 2
6 C 3
7 C <function amax at 0x108cf9048>
8 C 3
source
share