Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

174
Vistas
Converting NaN in dataframe to zero

I have dictionary and created Pandas using cars = pd.DataFrame.from_dict(cars_dict, orient='index') and sorted the index (columns in alphabetical order
cars = cars.sort_index(axis=1) After sorting I noticed the DataFrame has NaN and I wasn't sure if the really np.nan values? print(cars.isnull().any()) and all column shows false.

I have tried different method to convert those "NaN" values to zero which is what I want to do but non of them is working. I have tried replace and fillna methods and nothing works Below is sample of my dataframe..

            speedtest          size 
toyota       65                NaN 
honda        77                800 
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Either use replace or np.where on the values if they are strings:

df = df.replace('NaN', 0)

Or,

df[:] = np.where(df.eq('NaN'), 0, df)

Or, if they're actually NaNs (which, it seems is unlikely), then use fillna:

df.fillna(0, inplace=True)

Or, to handle both situations at the same time, use apply + pd.to_numeric (slightly slower but guaranteed to work in any case):

df = df.apply(pd.to_numeric, errors='coerce').fillna(0, downcast='infer')

Thanks to piRSquared for this one!

over 4 years ago · Santiago Trujillo Denunciar

0

@cs95's answer didn't work here.

Had to import numpy as np and use replace with np.Nan and inplace = True

import numpy as np

df.replace(np.NaN, 0, inplace=True)

Then all the columns got 0 instead of NaN.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda