Rashmi Singh
Rashmi Singh

Reputation: 11

dropna() in Python

I encountered a code with a Function that takes a row of data,drops all missing values, and checks if all remaining values are greater than or equal to 0:

def check_null_or_valid(row_data):

    no_na = row_data.dropna()[1:-1]
    numeric = pd.to_numeric(no_na)
    ge0 = numeric >= 0
    return ge0

I didn't understand the significance of [1:-1] after dropna().Please help me with this.

Upvotes: 1

Views: 1842

Answers (1)

Bart
Bart

Reputation: 10278

The [1:-1] simply slices the array, selecting all elements except the first and last one.

import numpy as np
a = np.arange(5)   # a is now array([0, 1, 2, 3, 4])
b = a[1:-1]        # b is now array([1, 2, 3])

With a minus sign, you can access elements relative to the end of the array. -1 is the last element, -2 the second to last, et cetera.

Upvotes: 2

Related Questions