# Columna Pandas fillna

Suraj Joshi 30 enero 2023

Este tutorial explica cómo podemos rellenar valores NaN con valores especificados utilizando el método `DataFrame.fillna()`.

En este artículo utilizaremos el siguiente DataFrame.

``````import numpy as np
import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
{
"Roll No": [501, 502, np.nan, 504, 505, 506],
"Name": ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
"Income(in \$)": [200, 400, np.nan, 30, np.nan, np.nan],
"Age": [17, 18, np.nan, 16, 18, np.nan],
}
)

print(student_df)
``````

Producción :

``````   Roll No      Name  Income(in \$)   Age
0    501.0  Jennifer         200.0  17.0
1    502.0    Travis         400.0  18.0
2      NaN       Bob           NaN   NaN
3    504.0      Emma          30.0  16.0
4    505.0      Luna           NaN  18.0
5    506.0     Anish           NaN   NaN
``````

## Método `DataFrame.fillna()`

### Sintaxis

``````DataFrame.fillna(
value=None, method=None, axis=None, inplace=False, limit=None, downcast=None
)
``````

El método `DataFrame.fillna()` nos permite rellenar los valores `NaN` del `DataFrame` con el `valor` o `method` especificado.

## Rellenar todo el DataFrame con el valor especificado utilizando el método `DataFrame.fillna()`

``````import numpy as np
import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
{
"Roll No": [501, 502, np.nan, 504, 505, 506],
"Name": ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
"Income(in \$)": [200, 400, np.nan, 30, np.nan, np.nan],
"Age": [17, 18, np.nan, 16, 18, np.nan],
}
)
filled_df = student_df.fillna(0)

print("DataFrame with NaN values")
print(student_df, "\n")

print("After applying fillna() to the DataFrame:")
print(filled_df, "\n")
``````

Producción :

``````DataFrame with NaN values
Roll No      Name  Income(in \$)   Age
0    501.0  Jennifer         200.0  17.0
1    502.0    Travis         400.0  18.0
2      NaN       Bob           NaN   NaN
3    504.0      Emma          30.0  16.0
4    505.0      Luna           NaN  18.0
5    506.0     Anish           NaN   NaN

After applying fillna() to the DataFrame:
Roll No      Name  Income(in \$)   Age
0    501.0  Jennifer         200.0  17.0
1    502.0    Travis         400.0  18.0
2      0.0       Bob           0.0   0.0
3    504.0      Emma          30.0  16.0
4    505.0      Luna           0.0  18.0
5    506.0     Anish           0.0   0.0
``````

Reemplaza todos los valores `NaN` del DataFrame `student_df` por `0` que se pasa como argumento al método `DataFrame.fillna()`.

``````import numpy as np
import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
{
"Roll No": [501, 502, np.nan, 504, 505, 506],
"Name": ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
"Income(in \$)": [200, 400, np.nan, 30, np.nan, np.nan],
"Age": [17, 18, np.nan, 16, 18, np.nan],
}
)
filled_df = student_df.fillna(method="ffill")

print("DataFrame with NaN values")
print(student_df, "\n")

print("After applying fillna() to the DataFrame:")
print(filled_df, "\n")
``````

``````DataFrame with NaN values
Roll No      Name  Income(in \$)   Age
0    501.0  Jennifer         200.0  17.0
1    502.0    Travis         400.0  18.0
2      NaN       Bob           NaN   NaN
3    504.0      Emma          30.0  16.0
4    505.0      Luna           NaN  18.0
5    506.0     Anish           NaN   NaN

After applying fillna() to the DataFrame:
Roll No      Name  Income(in \$)   Age
0    501.0  Jennifer         200.0  17.0
1    502.0    Travis         400.0  18.0
2    502.0       Bob         400.0  18.0
3    504.0      Emma          30.0  16.0
4    505.0      Luna          30.0  18.0
5    506.0     Anish          30.0  18.0
``````

Rellena todos los valores `NaN` en el `student_df` por el valor que precede al valor `NaN` en la misma columna que de valor `NaN`.

## Rellenar los valores `NaN` de la columna especificada con un valor especificado

Para rellenar los valores particulares con los valores especificados, pasamos un diccionario al método `fillna()` con el nombre de la columna como clave y el valor a utilizar para los valores `NaN` de esa columna como valor.

``````import numpy as np
import pandas as pd

roll_no = [501, 502, 503, 504, 505]

student_df = pd.DataFrame(
{
"Roll No": [501, 502, np.nan, 504, 505, 506],
"Name": ["Jennifer", "Travis", "Bob", "Emma", "Luna", "Anish"],
"Income(in \$)": [200, 400, np.nan, 300, np.nan, np.nan],
"Age": [17, 18, np.nan, 16, 18, np.nan],
}
)
filled_df = student_df.fillna({"Age": 17, "Income(in \$)": 300})

print("DataFrame with NaN values")
print(student_df, "\n")

print("After applying fillna() to the DataFrame:")
print(filled_df, "\n")
``````

Producción :

``````DataFrame with NaN values
Roll No      Name  Income(in \$)   Age
0    501.0  Jennifer         200.0  17.0
1    502.0    Travis         400.0  18.0
2      NaN       Bob           NaN   NaN
3    504.0      Emma         300.0  16.0
4    505.0      Luna           NaN  18.0
5    506.0     Anish           NaN   NaN

After applying fillna() to the DataFrame:
Roll No      Name  Income(in \$)   Age
0    501.0  Jennifer         200.0  17.0
1    502.0    Travis         400.0  18.0
2      NaN       Bob         300.0  17.0
3    504.0      Emma         300.0  16.0
4    505.0      Luna         300.0  18.0
5    506.0     Anish         300.0  17.0
``````

Rellena todos los valores `NaN` de la columna `Age` con el valor 17 y todos los valores `NaN` de la columna `Income(in \$)` con 300. Los valores `NaN` de la columna `Roll No` se dejan como están.

Suraj Joshi is a backend software engineer at Matrice.ai.