登录

pandas如何将生日按星座分组

如题,有两点不明,还请大牛指导:

1.如何忽略生日中的年份只比较月和日
2.摩羯座是跨越两个年份的(12-22,1-19),如何处理呢

series

       birthday    0    2008-06-08
    1    2008-11-09
    2    2013-10-12
    3    2002-09-28
    4    2007-10-24
    5    2012-08-27
    6    2005-08-22
    7    2008-04-12
    8    2001-06-29
    9    2009-07-23

dataframe

       constellationt    start    end    0    白羊座    3-21    4-19
    1    金牛座    4-20    5-20
    2    双子座    5-21    6-21
    3    巨蟹座    6-22    7-22
    4    狮子座    7-23    8-22
    5    处女座    8-23    9-22
    6    天秤座    9-23    10-23
    7    天蝎座    10-24    11-21
    8    射手座    11-22    12-21
    9    摩羯座    12-22    1-19
    10    水瓶座    1-20    2-18
    11    双鱼座    2-19    3-20


# Python
高洛峰 高洛峰 2737 天前 784 次浏览

全部回复(1) 我要回复

  • 三叔

    三叔2016-10-22 16:17:45

    这是一道数据整形题,既然用到了Pandas就尽量避免循环,多用向量化的思想。

    我们的目标是

         birthday     month   mon test
    0  2008-06-08  20080608   608  双子座
    1  2008-11-09  20081109  1109  天蝎座
    2  2013-10-12  20131012  1012  天秤座
    3  2002-09-28  20020928   928  天秤座
    4  2007-10-24  20071024  1024  天蝎座
    5  2012-08-27  20120827   827  处女座
    6  2005-08-22  20050822   822  狮子座
    7  2008-04-12  20080412   412  白羊座
    8  2001-06-29  20010629   629  巨蟹座
    9  2009-07-23  20090723   723  狮子座

    这个结构已经暴露了我们将要通过数值比较大小来确定所在星座的范围

    经过处理

       constellationt  start    end     s     e
    0             白羊座   3-21   4-19   321   419
    1             金牛座   4-20   5-20   420   520
    2             双子座   5-21   6-21   521   621
    3             巨蟹座   6-22   7-22   622   722
    4             狮子座   7-23   8-22   723   822
    5             处女座   8-23   9-22   823   922
    6             天秤座   9-23  10-23   923  1023
    7             天蝎座  10-24  11-21  1024  1121
    8             射手座  11-22  12-21  1122  1221
    9             摩羯座  12-22   1-19  1222   119
    10            水瓶座   1-20   2-18   120   218
    11            双鱼座   2-19   3-20   219   320

    这样的一个数据组用apply是很方便做到的,接下来构思星座范围判断的逻辑,

    分为三种,1 日期恰好在边界 2 日期在范围以内 3摩羯座的特殊情况

    def f(mon,cons):
        test = mon > cons[['s','e']]
        test0 = mon == cons[['s','e']]
        t0 = test0[test0.s|test0.e]
        t1 = test[test.s&-test.e]
        if not t0.empty:
            return cons.ix[t0.index].constellationt.iat[0]
        if not t1.empty:
            return cons.ix[t1.index].constellationt.iat[0]
        if test.s.all() and test.e.all():
            return cons.ix[[9]].constellationt.iat[0]

    最后

    birth['test'] = birth.mon.apply(lambda x:f(x,cons))

    这样就得到了如上结果


    回复
    0
  • 取消 回复 发送