Loading...

verticapy.vDataColumn.fillna

vDataColumn.fillna(val: int | float | str | datetime | date = None, method: Literal['auto', 'mode', '0ifnull', 'mean', 'avg', 'median', 'ffill', 'pad', 'bfill', 'backfill'] = 'auto', expr: str | StringSQL = '', by: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None, order_by: Annotated[str | list[str], 'STRING representing one column or a list of columns'] | None = None) vDataFrame

Fills missing elements in the vDataColumn with a user-specified rule.

Parameters

val: PythonScalar / date, optional

Value used to impute the vDataColumn.

method: dict, optional

Method used to impute the missing values.

  • auto:

    Mean for the numerical and Mode for the categorical vDataColumns.

  • bfill:

    Back Propagation of the next element (Constant Interpolation).

  • ffill:

    Propagation of the first element (Constant Interpolation).

  • mean:

    Average.

  • median:

    Median.

  • mode:

    Mode (most occurent element).

  • 0ifnull:

    0 when the vDataColumn is null, 1 otherwise.

expr: str, optional

SQL string.

by: SQLColumns, optional

vDataColumns used in the partition.

order_by: SQLColumns, optional

List of the vDataColumns used to sort the data when using TS methods.

Returns

vDataFrame

self._parent

Examples

We import verticapy:

import verticapy as vp

Hint

By assigning an alias to verticapy, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions from verticapy are used as intended without interfering with functions from other libraries.

For this example, we will use the Titanic dataset.

from verticapy.datasets import load_titanic

data = load_titanic()

Note

VerticaPy offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the VerticaPy environment.

We can see the count of each column to check if any column has missing values.

data.count()
count
"pclass"1234.0
"survived"1234.0
"name"1234.0
"sex"1234.0
"age"997.0
"sibsp"1234.0
"parch"1234.0
"ticket"1234.0
"fare"1233.0
"cabin"286.0
"embarked"1232.0
"boat"439.0
"body"118.0
"home.dest"706.0

From the above table, we can see that the count of boats is less than 1234. This suggests that it is missing some values.

Now we can use the fillna method to fill those values. Let’s use a custom function to fill these values.

data["age"].fillna(method = "avg", by = ["pclass", "sex"])
123
pclass
Int
100%
...
123
survived
Int
100%
Abc
home.dest
Varchar(100)
57%
11...0Montevideo, Uruguay
21...0Trenton, NJ
31...0[null]
41...0Montevideo, Uruguay
51...0Los Angeles, CA
61...0Lakewood, NJ
71...0Montreal, PQ
81...0Deephaven, MN / Cedar Rapids, IA
91...0New York, NY
101...0Scituate, MA
111...0[null]
121...0New York, NY
131...0[null]
141...0London / Middlesex
151...0Brighton, MA
161...0New York, NY
171...0New York, NY
181...0Springfield, MA
191...0Vancouver, BC
201...0Dorchester, MA

See also

vDataFrame.interpolate() : Fill missing values by interpolating.
vDataColumn.fill_outliers() : Fill the outliers using the input method.