将 python pandas df 中的列生日映射到占星术符号

我有一个包含个人生日的列的数据框。我想使用我找到的代码(如下)将该列映射到个人的占星术符号。我无法编写代码来创建变量。


我当前的数据框看起来像这样


     birthdate  answer  YEAR    MONTH-DAY

    1970-03-31    5    1970    03-31

    1970-05-25    9    1970    05-25

    1970-06-05    3    1970    06-05

    1970-08-28    2    1970    08-28

我发现创建日期映射函数的代码可在此网站上找到:https ://www.geeksforgeeks.org/program-display-astrological-sign-zodiac-sign-given-date-birth/


任何提示将不胜感激。


尚方宝剑之说
浏览 66回答 2
2回答

慕哥9229398

Series.dt.month_name用小写字符串更改先前的答案:def zodiac_sign(day, month):&nbsp;&nbsp; &nbsp; # checks month and date within the valid range&nbsp;&nbsp; &nbsp; # of a specified zodiac&nbsp;&nbsp; &nbsp; if month == 'december':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Sagittarius' if (day < 22) else 'capricorn'&nbsp; &nbsp; elif month == 'january':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Capricorn' if (day < 20) else 'aquarius'&nbsp; &nbsp; elif month == 'february':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Aquarius' if (day < 19) else 'pisces'&nbsp; &nbsp; elif month == 'march':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Pisces' if (day < 21) else 'aries'&nbsp; &nbsp; elif month == 'april':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Aries' if (day < 20) else 'taurus'&nbsp; &nbsp; elif month == 'may':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Taurus' if (day < 21) else 'gemini'&nbsp; &nbsp; elif month == 'june':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Gemini' if (day < 21) else 'cancer'&nbsp; &nbsp; elif month == 'july':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Cancer' if (day < 23) else 'leo'&nbsp; &nbsp; elif month == 'august':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Leo' if (day < 23) else 'virgo'&nbsp; &nbsp; elif month == 'september':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Virgo' if (day < 23) else 'libra'&nbsp; &nbsp; elif month == 'october':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'Libra' if (day < 23) else 'scorpio'&nbsp; &nbsp; elif month == 'november':&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; return 'scorpio' if (day < 22) else 'sagittarius'dates =&nbsp; pd.to_datetime(astrology['birthdate'])y = dates.dt.yearnow = pd.to_datetime('now').yearastrology = astrology.assign(month = dates.dt.month_name().str.lower(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;day = dates.dt.day,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;year = y.mask(y > now, y - 100))print (astrology)&nbsp; &nbsp; birthdate&nbsp; answer&nbsp; YEAR MONTH-DAY&nbsp; &nbsp;month&nbsp; day&nbsp; year0&nbsp; 1970-03-31&nbsp; &nbsp; &nbsp; &nbsp;5&nbsp; 1970&nbsp; &nbsp; &nbsp;03-31&nbsp; &nbsp;march&nbsp; &nbsp;31&nbsp; 19701&nbsp; 1970-05-25&nbsp; &nbsp; &nbsp; &nbsp;9&nbsp; 1970&nbsp; &nbsp; &nbsp;05-25&nbsp; &nbsp; &nbsp;may&nbsp; &nbsp;25&nbsp; 19702&nbsp; 1970-06-05&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; 1970&nbsp; &nbsp; &nbsp;06-05&nbsp; &nbsp; june&nbsp; &nbsp; 5&nbsp; 19703&nbsp; 1970-08-28&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; 1970&nbsp; &nbsp; &nbsp;08-28&nbsp; august&nbsp; &nbsp;28&nbsp; 1970astrology['sign'] = astrology.apply(lambda x: zodiac_sign(x['day'], x['month']), axis=1)print (astrology)&nbsp; &nbsp; birthdate&nbsp; answer&nbsp; YEAR MONTH-DAY&nbsp; &nbsp;month&nbsp; day&nbsp; year&nbsp; &nbsp; sign0&nbsp; 1970-03-31&nbsp; &nbsp; &nbsp; &nbsp;5&nbsp; 1970&nbsp; &nbsp; &nbsp;03-31&nbsp; &nbsp;march&nbsp; &nbsp;31&nbsp; 1970&nbsp; &nbsp;aries1&nbsp; 1970-05-25&nbsp; &nbsp; &nbsp; &nbsp;9&nbsp; 1970&nbsp; &nbsp; &nbsp;05-25&nbsp; &nbsp; &nbsp;may&nbsp; &nbsp;25&nbsp; 1970&nbsp; gemini2&nbsp; 1970-06-05&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; 1970&nbsp; &nbsp; &nbsp;06-05&nbsp; &nbsp; june&nbsp; &nbsp; 5&nbsp; 1970&nbsp; Gemini3&nbsp; 1970-08-28&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; 1970&nbsp; &nbsp; &nbsp;08-28&nbsp; august&nbsp; &nbsp;28&nbsp; 1970&nbsp; &nbsp;virgo

MYYA

您可以将zodiac_sign函数应用于数据框 -import pandas as pdfrom io import StringIO# Samplex = StringIO("""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""")df = pd.read_csv(x, sep=',')df['birthdate'] = pd.to_datetime(df['birthdate'])df['zodiac_sign'] = df['birthdate'].apply(lambda x: zodiac_sign(x.day, x.strftime("%B").lower()))print(df)输出:&nbsp; &nbsp;birthdate&nbsp; answer&nbsp; YEAR MONTH-DAY zodiac_sign0 1970-03-31&nbsp; &nbsp; &nbsp; &nbsp;5&nbsp; 1970&nbsp; &nbsp; &nbsp;03-31&nbsp; &nbsp; &nbsp; &nbsp;aries1 1970-05-25&nbsp; &nbsp; &nbsp; &nbsp;9&nbsp; 1970&nbsp; &nbsp; &nbsp;05-25&nbsp; &nbsp; &nbsp; gemini2 1970-06-05&nbsp; &nbsp; &nbsp; &nbsp;3&nbsp; 1970&nbsp; &nbsp; &nbsp;06-05&nbsp; &nbsp; &nbsp; Gemini3 1970-08-28&nbsp; &nbsp; &nbsp; &nbsp;2&nbsp; 1970&nbsp; &nbsp; &nbsp;08-28&nbsp; &nbsp; &nbsp; &nbsp;virgo
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python