Python Forum
Function question using Pandas
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function question using Pandas
#1
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()
Reply
#2
with 10 threads and 27 posts so far you should know to use python tags when posting code
Reply
#3
(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?
Reply
#4
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.
Reply
#5
(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 :)
Reply
#6
(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..
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply
#7
(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.
Reply
#8
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help to control pandas.melt function vanphuht91 0 704 Jun-17-2023, 08:41 AM
Last Post: vanphuht91
  pandas df inside a df question mbaker_wv 4 1,185 Dec-25-2022, 01:11 AM
Last Post: mbaker_wv
  Pandas usecols question rsearing 1 1,243 Aug-20-2022, 10:10 PM
Last Post: jefsummers
  Simple pandas question mcva 4 2,648 Dec-17-2021, 04:47 PM
Last Post: mcva
  Pandas replace function not working on datafram with floats bcrypto 1 2,879 Apr-12-2021, 08:59 PM
Last Post: bcrypto
  Pandas question new2datasci 0 1,950 Jan-10-2021, 01:29 AM
Last Post: new2datasci
  Pandas merge question smw10c 2 5,722 Jul-02-2020, 06:56 PM
Last Post: hussainmujtaba
  What is the mechanism of numpy function returning pandas object? Ibaraki 2 2,499 Apr-04-2020, 10:57 PM
Last Post: Ibaraki
  Counting Criteria in Pandas Question Koenig 1 2,165 Sep-30-2019, 05:16 AM
Last Post: perfringo
  how to apply user defined function to Pandas DataFrame evelynow 3 7,622 Aug-20-2019, 11:35 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020