Detailed explanation of pandas data to determine whether NaN value

  • 2021-01-25 07:36:16
  • OfStack

In actual projects, there is such a requirement to map the value of a column into category-type data. At this time, we need to divide the range into equal frequency or equal distance.

The specific approach can first look at the specific distribution of some features, and then we choose the appropriate threshold for segmentation.


def age_map(x):
 if x < 26:
  return 0
 elif x >=26 and x <= 35:
  return 1
 elif x > 35 and x <= 45:
  return 2
 elif pd.isnull(x): # Determine whether or not NaN Value, ==  and in  I can't tell 
  return 3
 else:
  return 4

That is to say, using pandas's own function:


pd.isnull(x) 

Finally, we can apply the map function:


data['age'] = data['birth_year'].map(age_map)

Related articles: