The zodiac sign thing is not working. Not sure what is wrong with my code

from datetime import datetime

def zodiac_sign(input): 

    try:

      date = datetime.strptime(input, '%Y-%m-%d')

      

      if date.month == 12: 

        astro_sign = 'Sagittarius' if (date.day < 22) else 'Capricorn'

          

      elif date.month == 1: 

        astro_sign = 'Capricorn' if (date.day < 20) else 'Aquarius'

          

      elif date.month == 2: 

        astro_sign = 'Aquarius' if (date.day < 19) else 'Pisces'

          

      elif date.month == 3: 

        astro_sign = 'Pisces' if (date.day < 21) else 'Aries'

          

      elif date.month == 4: 

        astro_sign = 'Aries' if (date.day < 20) else 'Taurus'

          

      elif date.month == 5: 

        astro_sign = 'Taurus' if (date.day < 21) else 'Gemini'

          

      elif date.month == 6: 

        astro_sign = 'Gemini' if (date.day < 21) else 'Cancer'

          

      elif date.month == 7: 

        astro_sign = 'Cancer' if (date.day < 23) else 'Leo'

          

      elif date.month == 8: 

        astro_sign = 'Leo' if (date.day < 23) else 'Virgo'

          

      elif date.month == 9: 

        astro_sign = 'Virgo' if (date.day < 23) else 'Libra'

          

      elif date.month == 10: 

        astro_sign = 'Libra' if (date.day < 23) else 'Scorpio'

          

      elif date.month == 11: 

        astro_sign = 'Scorpio' if (date.day < 22) else 'Sagittarius'

    except:

      astro_sign = None

                

    return(astro_sign)

largedf['Astro sign'] = zodiac_sign(largedf['born'])

The above is the code. This is the output I am getting
largedf[['born','Astro sign']]

image

It works when I do it individually though

image

Hey there! Instead of attaching a new column with the zodiac sign for all rows, an easier way is to directly put the condition for someone being Scorpio within your SQL query.

For example, something like this:
Select singers from table where
birthday.month = 10 and
birthday.month = 11 and
birthday.date <= 20 and
birthday.date > 21

This will save you space and computing time.

Also for the error you’re getting in the code you have written, it’s because you need to go through each row of the dataframe and apply the function.

You’re able to get the right answer when you do it individually but when you are trying to do it for all rows at once, I can see you’re not getting.

Try
largedf['Astro Sign'] = largedf['Astro Sign'].apply(lambda x: zodiac_sign(x))

Let me know if this is working for you Vishal

@sakzsee Hey thanks. This works. I did try something similar before doing that. Why does what I did work in this case but on the largedf?

@arya-mohan Can you please clarify this? Thanks!

@vishalbx1 Not sure I understand the context of this discussion. Let us connect on Monday during office hours?