Pandas Reemplazar NaN con cadena en blanco / vacía
Tengo un Dataframe Pandas como se muestra a continuación:
1 2 3
0 a NaN read
1 b l unread
2 c NaN read
Quiero eliminar los valores NaN con una cadena vacía para que se vea así:
1 2 3
0 a "" read
1 b l unread
2 c "" read
90
Author: user1452759, 2014-11-10
5 answers
import numpy as np
df1 = df.replace(np.nan, '', regex=True)
Esto podría ayudar. Reemplazará todas las NAN con una cadena vacía.
97
Author: nEO,
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-03-08 14:05:51
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-03-08 14:05:51
Un poco más corto es:
df = df.fillna('')
Esto llenará na (por ejemplo, NaN) con ".
Editar: Si desea llenar una sola columna, puede usar:
df.column1 = df.column1.fillna('')
170
Author: fantabolous,
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-04-01 02:19:45
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-04-01 02:19:45
Si está leyendo el dataframe de un archivo (digamos CSV o Excel), use :
-
df.read_csv(path , na_filter=False)
df.read_excel(path , na_filter=False)
Este considerará automáticamente los campos vacíos como cadenas vacías ' '
Si ya tiene el Dataframe
df = df.replace(np.nan, '', regex=True)
df = df.fillna('')
39
Author: Natesh bhat,
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-07-25 10:09:18
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-07-25 10:09:18
Si está convirtiendo Dataframe a Json: NaN dará error por lo que la mejor solución es en este caso de uso es reemplazar NaN con None. Aquí está cómo: df1 = df.donde ((pd.notnull (df)), None)
0
Author: Dinesh Khetarpal,
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
2018-03-15 20:48:54
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
2018-03-15 20:48:54
Use un formmatter. Además, no modificas tu DataFrame:
df = pd.DataFrame({
'A': ['a', 'b', 'c'],
'B': [np.nan, 1, np.nan],
'C': ['read', 'unread', 'read']})
print df.to_string(
formatters={'B': lambda x: '' if pd.isnull(x) else '{:.0f}'.format(x)})
Para obtener:
A B C
0 a read
1 b 1 unread
2 c read
0
Author: Steve Schulist,
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
2018-06-21 22:41:45
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
2018-06-21 22:41:45