I am trying to replace None (not recognized as a string) with nan — and fill those nans with the mode of the field, but when I further condense the field — None appears back in the output. What am I missing?
JavaScript
x
45
45
1
final_df.Current_Housing_Living_Status__c.unique()
2
3
4
Ouput: array([nan, 'Living with family', 'Rent', 'Living with friends/others',
5
'Homeless', None, 'Emergency Shelter', 'Own', 'Supportive Housing',
6
'Transitional Housing', 'MHSA treatment facility'], dtype=object)
7
8
final_df.replace(to_replace=[None], value=np.nan, inplace=True)
9
10
Ouput: array([nan, 'Living with family', 'Rent', 'Living with friends/others',
11
'Homeless', 'Emergency Shelter', 'Own', 'Supportive Housing',
12
'Transitional Housing', 'MHSA treatment facility'], dtype=object)
13
14
final_df['Current_Housing_Living_Status__c'].fillna(final_df['Current_Housing_Living_Status__c'].mode()[0], inplace = True)
15
16
17
#Some Dimension Reduction
18
19
Own=['Own']
20
Rent=['Rent']
21
Homeless=['Homeless','Emergency Shelter', 'Supportive Housing', 'Transitional Housing']
22
Live_with_Others=['Living with family', 'Living with friends/others']
23
Treatment_Facility=['MHSA treatment facility']
24
25
def reduce_housing_status(x):
26
if x in Own:
27
return 'Own'
28
elif x in Rent:
29
return 'Rent'
30
elif x in Homeless:
31
'Homeless'
32
elif x in Live_with_Others:
33
return 'Live_with_Others'
34
elif x in Treatment_Facility:
35
return 'Treatment_Facility'
36
else:
37
return x
38
39
final_df['Current_Housing_Living_Status__c'] = final_df['Current_Housing_Living_Status__c'].apply(reduce_housing_status)
40
41
final_df.Current_Housing_Living_Status__c.unique()
42
43
Ouput: array(['Rent', 'Live_with_Others', None, 'Own', 'Treatment_Facility'],
44
dtype=object)
45
None is back… What am I missing/doing wrong here? If I rerun that last section, None will disappear – but its worrisome to see it appear in the output, then disappear upon a second run.
Advertisement
Answer
Inside your reduce_housing_status
function you forgot to add a return statement when x in Homeless
:
JavaScript
1
3
1
elif x in Homeless:
2
'Homeless'
3
Which means you’re implicitly returning None