tekasian / intro-data-capstone-biodiversity

0 stars 0 forks source link

Unnecessary lambda #2

Open asadmehdi785 opened 6 years ago

asadmehdi785 commented 6 years ago

https://github.com/tekasian/intro-data-capstone-biodiversity/blob/297a56430354b6ef4894b9f0bcaa2b78767381c9/Biodiversity_Capstone_Project_Bryan_Leung/biodiversity.py#L157

This will work fine, but we can achieve the same result by using a simpler line of code:

species['is_protected'] = species.conservation_status != 'No Intervention'

This means that when species.conservation_status != 'No Intervention', it will return True, and False otherwise.

tekasian commented 6 years ago

MUCH better! This is a great tip as I still feel somewhat nervous about imputing the lambda function incorrectly. The above code is pretty much saying the expression in my head and typing it out.

tekasian commented 6 years ago

Hi Asad,

When I typed in the simplified code, I get an attribute error further down the script:

`--------------------------------------------------------------------------- AttributeError Traceback (most recent call last)

in () ----> 1 category_pivot['percent_protected'] = category_pivot.protected / (category_pivot.protected + category_pivot.not_protected) /anaconda2/lib/python2.7/site-packages/pandas/core/generic.pyc in __getattr__(self, name) 3612 if name in self._info_axis: 3613 return self[name] -> 3614 return object.__getattribute__(self, name) 3615 3616 def __setattr__(self, name, value): AttributeError: 'DataFrame' object has no attribute 'protected' ` If I leave my original lambda statement alone, the script works just fine. Comments? Thanks, Bryan
asadmehdi785 commented 6 years ago

Can you try to restart the kernel by going to Kernel -> Restart & Run All? Sometimes that helps when it doesn't recognize certain variables.

tekasian commented 6 years ago

Hmm, it gives the same message.

asadmehdi785 commented 6 years ago

Okay. In the step before, can you print out category_pivot and show what it looks like for you?

tekasian commented 6 years ago

screen shot 2018-05-20 at 5 09 34 pm

tekasian commented 6 years ago

Unfortunately, it won't give me the screen from my original submission:

screen shot 2018-05-20 at 5 10 12 pm

asadmehdi785 commented 6 years ago

Ah sorry, I should have been more clear. Can you add a statement to output category_pivot after this code:

category_pivot = category_pivot.rename(columns={'False': 'not_protected', 'True': 'protected'})

So you can change that part to run it like this:

category_pivot = category_pivot.rename(columns={'False': 'not_protected', 'True': 'protected'})
print category_pivot

Can you show what is printed when you run that?

tekasian commented 6 years ago

It looks like the the above script isn't renaming the columns to their new names, which the following code isn't able to find a category named 'protected'.

tekasian commented 6 years ago

category_pivot = category_pivot.rename(columns={'False': 'not_protected', 'True': 'protected'}) print category_pivot

tekasian commented 6 years ago

is_protected category False True 0 Amphibian 72 7 1 Bird 413 75 2 Fish 115 11 3 Mammal 146 30 4 Nonvascular Plant 328 5 5 Reptile 73 5 6 Vascular Plant 4216 46

tekasian commented 6 years ago

I changed one thing to make the fixed lambda code work: Previously, I did not use the columns property to rename the categories True and False. After changing the script to: category_pivot.columns = {'category', 'not-protected', 'protected'] our following script finally works.

screen shot 2018-05-20 at 5 50 38 pm

asadmehdi785 commented 6 years ago

Ah okay! I was wondering about that piece of code, honestly I didn't think it made a difference but I guess it's better to use the columns property here. Nice job with that!