Skip to main content
1 de 2
HeytalePazguato
  • 11k
  • 3
  • 13
  • 34

Buen día,

Tienes varias opciones para hacerlo

Opción 1

Usar isnan() de numpy

import numpy as np
from numpy import nan

my_array = np.array([10, 25, nan, 15,nan,nan]

new_array = my_array[np.logical_not(np.isnan(my_array))]
print(new_array)

La salida es [10. 25. 15.]

Opción 2

Usar isnan() de math

import math
from numpy import nan

my_list = [10, 25, nan, 15,nan,nan]

new_list = [item for item in my_list if not(math.isnan(item)) == True]
print(new_list)

Opción 3

Usar isnull() de pandas

import pandas as pd
from numpy import nan

my_list = ['Mike', 'Harry', nan, 'Emma',nan,nan]

new_list = [item for item in my_list if not(pd.isnull(item)) == True]
print(new_list)

Opción 4

Usar isna() de pandas

import pandas as pd
from numpy import nan
my_list = ['Mike', 'Harry', nan, 'Emma',nan,nan]
new_list = [item for item in my_list if not(pd.isna(item)) == True]
print(new_list)

Opción 5

Usar un ciclo (Bucle) for

from numpy import nan
my_list = ['Mike', 'Harry', nan, 'Emma',nan,nan]
new_list = []
for item in my_list:
  if str(item) != 'nan':
    new_list.append(item)
 
print(new_list)

Opción 6

Usar listcomprehension

from numpy import nan
my_list = ['Mike', 'Harry', nan, 'Emma',nan,nan]
new_list = [item for item in my_list if str(item) != 'nan']
print(new_list)
HeytalePazguato
  • 11k
  • 3
  • 13
  • 34