Map column birthdates in python pandas df to astrology signs

2024/11/15 0:04:46

I have a dataframe with a column that includes individuals' birthdays. I would like to map that column to the individuals' astrology sign using code I found (below). I am having trouble writing the code to creat the variables.

My current dataframe looks like this

     birthdate  answer  YEAR    MONTH-DAY1970-03-31    5    1970    03-311970-05-25    9    1970    05-251970-06-05    3    1970    06-051970-08-28    2    1970    08-28

The code I found that creates a function to map the dates is available at this website: https://www.geeksforgeeks.org/program-display-astrological-sign-zodiac-sign-given-date-birth/

Any tips would be appreciated.

Answer

Change previous answer by Series.dt.month_name with lowercase strings:

def zodiac_sign(day, month): # checks month and date within the valid range # of a specified zodiac if month == 'december': return 'Sagittarius' if (day < 22) else 'capricorn'elif month == 'january': return 'Capricorn' if (day < 20) else 'aquarius'elif month == 'february': return 'Aquarius' if (day < 19) else 'pisces'elif month == 'march': return 'Pisces' if (day < 21) else 'aries'elif month == 'april': return 'Aries' if (day < 20) else 'taurus'elif month == 'may': return 'Taurus' if (day < 21) else 'gemini'elif month == 'june': return 'Gemini' if (day < 21) else 'cancer'elif month == 'july': return 'Cancer' if (day < 23) else 'leo'elif month == 'august': return 'Leo' if (day < 23) else 'virgo'elif month == 'september': return 'Virgo' if (day < 23) else 'libra'elif month == 'october': return 'Libra' if (day < 23) else 'scorpio'elif month == 'november': return 'scorpio' if (day < 22) else 'sagittarius'

dates =  pd.to_datetime(astrology['birthdate'])
y = dates.dt.year
now = pd.to_datetime('now').year
astrology = astrology.assign(month = dates.dt.month_name().str.lower(),day = dates.dt.day,year = y.mask(y > now, y - 100))
print (astrology)birthdate  answer  YEAR MONTH-DAY   month  day  year
0  1970-03-31       5  1970     03-31   march   31  1970
1  1970-05-25       9  1970     05-25     may   25  1970
2  1970-06-05       3  1970     06-05    june    5  1970
3  1970-08-28       2  1970     08-28  august   28  1970

astrology['sign'] = astrology.apply(lambda x: zodiac_sign(x['day'], x['month']), axis=1)
print (astrology)birthdate  answer  YEAR MONTH-DAY   month  day  year    sign
0  1970-03-31       5  1970     03-31   march   31  1970   aries
1  1970-05-25       9  1970     05-25     may   25  1970  gemini
2  1970-06-05       3  1970     06-05    june    5  1970  Gemini
3  1970-08-28       2  1970     08-28  august   28  1970   virgo
https://en.xdnf.cn/q/120491.html

Related Q&A

How to use python pandas to find a specific string in various rows

I am trying to do my taxes. I have over 2,000 rows of data in a CSV of orders. I am trying to just count and print the rows that contain "CA" so I know the ones I need to pay sales tax on. I …

How to cast a list to a dictionary

I have a list as a input made from tuples where the origin is the 1st object and the neighbour is the 2nd object of the tuple. for example :inp : lst = [(a,b),(b,a),(c,),(a,c)] out : {a: (a, [b, c]), …

Couldnt get rid of (_tkinter.TclError: bad event type or keysym UP) problem

I am running a Linux mint for the first time . I tried coding a python problem but for two days I am continiously facing problems due to Linux interface please This is my code:-import turtleimport time…

WMI lib to start windows service remotely

How do I start a service using the WMI library? The code below throws the exception: AttributeError: list object has no attribute StopServiceimport wmi c = wmi.WMI (servername,user=username,password=p…

TutorialsPoint - Flask – SQLAlchemy not working

I did all that was stated on tutorial point (just copied and pasted), but when I tried to add a student entry,i.e. ‘Add Student’ it givesBad Request The browser (or proxy) sent a request that this se…

Implement f-string like magic to pimp Djangos format_html()

I would like to pimp format_html() of Django. It already works quite nicely, but my IDE (PyCharm) thinks the variables are not used and paints them in light-gray color:AFAIK f-strings use some magic re…

Selecting a set of numbers from a list which add up to a given value [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

List of even numbers at even number indexes using list comprehension [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 9…

How do I check if a list of lists exists in a list of lists?

I got a list of lists b and I want to check if they exist in list a which is also a list of lists. Im currently using the following method which is quite time-consuming. Is there a faster way? b = [[…

How do i print out a number triangle in python? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…