0

I'm trying to create a new "category" based on the value in another column (looping through that column). Here is my code.

def minority_category(minority_col):
for i in minority_col:
    if i <= 25:
        return '<= 25%'
    if i > 25 and i <= 50:
        return '<= 50%'
    if i > 50 and i <= 75:
        return '<= 75%'
    if i > 75 and i <= 100:
        return '<= 100%'
    return 'Unknown'

However, the result is '<=75%' in the entire new column. Based on examples I've seen, my code looks right. Can anyone point out something wrong with the code? Thank you.

Drew M
  • 51
  • 1
  • 5
  • 1
    Is the code indented correctly? – T.Woody Aug 27 '18 at 19:59
  • What is being passed to `minority_category`? – T.Woody Aug 27 '18 at 20:00
  • Can you check what is the value of minority_col by printing it? Also save the value being returned and print it before returning that way you can compare the input and output of the function and verify it. Please read https://www.codementor.io/allisonf/how-to-debug-python-code-beginners-print-line-du107ltvx on how to use print statements for debugging. – vaichidrewar Aug 27 '18 at 20:38
  • T.Woody , yes. It just didn't paste properly. And I'm passing in a column. So a series is being passed in. – Drew M Aug 27 '18 at 21:28
  • Is this pandas? It is not labeled as such and it is not a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). If you work with pandas, I assume you [try to achieve something like this](https://stackoverflow.com/a/26887820/8881141). – Mr. T Aug 28 '18 at 07:09

0 Answers0