Here is the dataframe with some NaN
values,
data = {'Number':[100,None,None,200,150,None,100,120,110,210,120],
'Street':['A','B','C','D','C','D','A','B','B','D','B']}
df =pd.DataFrame(data)
df
Output:
Number Street
0 100.0 A
1 NaN B
2 NaN C
3 200.0 D
4 150.0 C
5 NaN D
6 100.0 A
7 120.0 B
8 110.0 B
9 210.0 D
10 120.0 B
I want to replace the NaN
values of the column 'Number' by the mode of the same column with respect to the column 'Street'.
The output I need is:
Number Street
0 100 A
1 120 B
2 150 C
3 200 D
4 150 C
5 200 D
6 100 A
7 120 B
8 110 B
9 210 D
10 120 B
Explanation:
For example, consider row 1 which has a NaN
value in the column Number and the corresponding Street is B
. The NaN
value of Number should be replaced by 120.0
which is the mode of the column Number with respect to Street. Because, the other values for the column Number for Street B
are 120.0, 110.0
and 120.0
(look at row nos 7,8,10), and the mode for this is 120.0
.