1

I've got a csv file where some of data is empty. When I use an if statement, the NaN data is included to else statement.

def warna_kota(population):
    if population < 10000:
        return 'green'
    elif 10000 <= population < 20000:
        return 'orange'
    elif 20000 <= population < 30000:
        return 'brown'
    elif 40000 <= population < 50000:
        return 'yellow' 
    elif 60000 <= population < 70000:
        return 'blue'
    elif 80000 <= population < 90000:
        return 'gold'
    elif 90000 <= population < 100000:
        return 'pink'
    else:
        return 'red'

The result shows that NaN data is red, I tried to use this,

if population = NaN:
    return 'grey'
elif:
    ...

However, when I try to run the code, it produces an error. The NaN data is empty, and I want to separate NaN data from the else statement. How would I do that?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Hendra Sirait
  • 57
  • 1
  • 12

1 Answers1

2

Use math.isnan(population) for that check.

import math

# ...

if math.isnan(population):
    return 'grey'
AKX
  • 152,115
  • 15
  • 115
  • 172