Filtrar columna Pyspark dataframe con valor None
Estoy tratando de filtrar un dataframe PySpark que tiene None
como un valor de fila:
df.select('dt_mvmt').distinct().collect()
[Row(dt_mvmt=u'2016-03-27'),
Row(dt_mvmt=u'2016-03-28'),
Row(dt_mvmt=u'2016-03-29'),
Row(dt_mvmt=None),
Row(dt_mvmt=u'2016-03-30'),
Row(dt_mvmt=u'2016-03-31')]
Y puedo filtrar correctamente con un valor de cadena:
df[df.dt_mvmt == '2016-03-31']
# some results here
Pero esto falla:
df[df.dt_mvmt == None].count()
0
df[df.dt_mvmt != None].count()
0
Pero definitivamente hay valores en cada categoría. ¿Qué está pasando?
3 answers
Puede utilizar Column.isNull
/ Column.isNotNull
:
df.where(col("dt_mvmt").isNull())
df.where(col("dt_mvmt").isNotNull())
Si desea simplemente soltar NULL
valores, puede usar na.drop
con el argumento subset
:
df.na.drop(subset=["dt_mvmt"])
Las comparaciones basadas en igualdad con NULL
no funcionarán porque en SQL NULL
no está definido, por lo que cualquier intento de compararlo con otro valor devuelve NULL
:
sqlContext.sql("SELECT NULL = NULL").show()
## +-------------+
## |(NULL = NULL)|
## +-------------+
## | null|
## +-------------+
sqlContext.sql("SELECT NULL != NULL").show()
## +-------------------+
## |(NOT (NULL = NULL))|
## +-------------------+
## | null|
## +-------------------+
El único método válido para comparar el valor con NULL
es IS
/ IS NOT
que son equivalentes a la isNull
/ isNotNull
llamadas de método.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-16 20:57:27
Intente usar la función isNotNull.
df.filter(df.dt_mvmt.isNotNull()).count()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-05-16 20:50:05
Para obtener entradas cuyos valores en la columna dt_mvmt
no son null tenemos
df.filter("dt_mvmt is not NULL")
Y para las entradas que son null tenemos
df.filter("dt_mvmt is NULL")
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-09 02:37:06