Python Forum

Full Version: Function question using Pandas
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I hope you are all having a good day. I am using Datacamp to learn Python and the instructor provided code without explaining each line. There was very little explanation as well in previous lessons to allow us to think critically and understand it on our own. The code below is 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. Can someone break down line by line what is happening. I'm particularly confused by the [1:-1] aspect.

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
assert g1800s.iloc[:, 1:].apply(check_null_or_valid, axis=1).all().all()
with 10 threads and 27 posts so far you should know to use python tags when posting code
(Apr-25-2017, 05:12 PM)buran Wrote: [ -> ]with 10 threads and 27 posts so far you should know to use python tags when posting code
(Apr-25-2017, 05:12 PM)buran Wrote: [ -> ]with 10 threads and 27 posts so far you should know to use python tags when posting code

What do you mean by Python tags?
when you click post thread there is template my code goes here. it is already between [python] tags.
when you click new reply there are some buttons. use the one with python symbol. there are also output , error, inline tags, etc.
this preserve the indentation of the code.
(Apr-25-2017, 05:19 PM)buran Wrote: [ -> ]when you click post thread there is template my code goes here. it is already between [python] tags. when you click new reply there are some buttons. use the one with python symbol. there are also output , error, inline tags, etc.

I never noticed that. I will do that in the future :)
(Apr-25-2017, 05:20 PM)smw10c Wrote: [ -> ]I never noticed that. I will do that in the future :)

Why wait Naughty ? You may edit your posts, you know..
(Apr-25-2017, 06:17 PM)volcano63 Wrote: [ -> ]
(Apr-25-2017, 05:20 PM)smw10c Wrote: [ -> ]I never noticed that. I will do that in the future :)
Why wait Naughty ? You may edit your posts, you know..

I believe I did it correctly. Please let me know if I didn't.
I know the question is from 2017, but it may help future readers.

no_na = row_data.dropna()[1:-1]
.dropna() remove missing values from the dataframe. I don't believe that the [1:-1] is necessary, but that is a range, from the second to the last one.


numeric = pd.to_numeric(no_na)
transform all those values to a numeric type with the .to_numeric()


ge0 = numeric >= 0
check if all the items are >= 0