Skip to main content

Fill NA values

Use this function to fill in NA values in a sequential manner up to a specified limit. When using this function, you will specify the following:

  • Whether the method to fill the NA values should go forward (default) or backward.
  • Whether the NA values should be filled along rows (default) or columns.
  • The maximum number of consecutive NA values to fill (defaults to 1).
import h2o
h2o.init()

# Create a random data frame with 10 rows and 3 columns.
# Specify that no more than 20% of the values are NAs.
df = h2o.create_frame(rows=10,
cols=3,
real_fraction=1.0,
real_range=100,
missing_fraction=0.2,
seed=123)
df
C1 C2 C3
-------- -------- --------
nan nan -77.1047
-93.6409 -13.6593 57.4439
-93.71 25.4342 39.1013
-95.8291 -92.4271 55.4314
84.6372 -43.4759 53.1715
-57.9583 27.4148 -26.9013
83.0921 -62.7819 -91.9426
-77.9814 64.3228 -93.954
nan -80.6142 nan
27.1672 60.5492 -13.2275

[10 rows x 3 columns]

# Forward fill a row. In Python, the values for axis are 0 (row-wise) and 1 (column-wise)
filled = df.fillna(method="forward",axis=0,maxlen=1)
filled

filled
C1 C2 C3
-------- -------- --------
nan nan -77.1047
-93.6409 -13.6593 57.4439
-93.71 25.4342 39.1013
-95.8291 -92.4271 55.4314
84.6372 -43.4759 53.1715
-57.9583 27.4148 -26.9013
83.0921 -62.7819 -91.9426
-77.9814 64.3228 -93.954
-77.9814 -80.6142 -93.954
27.1672 60.5492 -13.2275

[10 rows x 3 columns]

Feedback