I am creating a new column in a dataframe based on a conditional function. In the column I am mapping from, there are multiple NaN values. If a NaN value is present in the original column, I also want this to be present in my new column. As an example, my starting point is this:
Original
0 1
1 2
2 3
3 4
4 5
5 6
6 Nan
7 8
8 9
9 10
Here is a sample of the code I initially run which gives the following result (clearly):
def get_value(range):
if range < 2:
return 'Below 2'
elif range < 8:
return 'Between 2 and 8'
else:
return 'Above 8'
df_sample['new_col'] = df_sample.apply(lambda x: get_value(x['Original']), axis=1)
Original new_col
0 1.0 Below 2
1 2.0 Between 2 and 8
2 3.0 Between 2 and 8
3 4.0 Between 2 and 8
4 5.0 Between 2 and 8
5 6.0 Between 2 and 8
6 NaN Above 8
7 8.0 Above 8
8 9.0 Above 8
9 10.0 Above 8
Here, index 6 should show NaN.
I have tried including elif range == np.Nan: in my function but this didn't work.
I then tried the following based on a recommendation on Stackoverflow:
df_sample['new_col'] = df_sample.apply(lambda x: get_value(x) if(np.all(pd.notnull(x['Original']))) else x, axis = 1)
But this returns an error at the first NaN index in my dataframe.