python - Pandas:识别范围内值的重叠成员资格

标签 python pandas

我正在研究一个包含成员(member)资格开始和结束日期(例如,2003 年 12 月 3 日和 2007 年 10 月 23 日)的数据集,并且我试图隔离每年的成员(member)资格(在上面的示例中,我将查找 2003 年, 2004、2005、2006、2007)。

现在,我的代码只能识别第一年的成员资格,这没有帮助,因为我可以通过查看加入年份来获得该成员资格。

year_list = [2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009]

for year in year_list:

    mem_year_list = []

    for x in dfy.join_year:
        if x >= year | x < (year+1):
            mem_year_list.append(1)
        else:
            mem_year_list.append(0) 

我觉得我可能错过了一段声明,但我一直无法弄清楚它,并且如果发现那不是缺失的部分,我不会感到惊讶。

数据开始如下:

mem_no  status  animal  join_year  exp_year         
00004   Active  Lark    12-2-02    10-23-07          
00101   Expired Parrot  4-1-03     2-1-16       
00118   Crunchy Frog    10-8-01    2-22-02      
00121   Grumpy  Panda   5-1-03     3-1-04    

最终看起来像这样:

mem_no  status  animal  join_year  exp_year  mem_02 mem_03 mem_04 mem_05    
00004   Active  Lark    12-2-02    10-23-07  1    0      0      0        
00101   Expired Parrot  4-1-03     2-1-16    0    1      0      0       
00118   Crunchy Frog    10-8-01    2-22-02   1    0      0      0       
00121   Grumpy  Panda   5-1-03     3-1-04    0    1      0      0

但我希望它最终是这样的:

mem_no  status  animal  join_year  exp_year  mem_02 mem_03 mem_04 mem_05    
00004   Active  Lark    12-2-02    10-23-07  1    1      1      1        
00101   Expired Parrot  4-1-03     2-1-16    0    1      1      1       
00118   Crunchy Frog    10-8-01    2-22-02   1    0      0      0       
00121   Grumpy  Panda   5-1-03     3-1-04    0    1      1      0
<小时/>

Roman 给出了一个很好的答案,只需要进行一些调整:

dfy['joined'] = pd.to_datetime(dfy['joined'])
dfy['exp_date'] = pd.to_datetime(dfy['exp_date'])

year_list = [2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,\
             2012, 2013, 2014, 2015]

for year in year_list:
# code to isolate prior years

    dfy['mem_' + str(year)] = dfy.apply(lambda x: x['joined'].year\
       <= year and x['exp_date'].year>= year, axis=1).astype('int')

# code to isolate current year

dfy['mem_2016'] = dfy.apply(lambda x: x['exp_date'].year\
       <= 2016, axis=1).astype('int')

最佳答案

>>> for year in year_list:
...     dfy['mem_' + str(year)] = dfy.apply(lambda x: x['join'].year <= year and x['end'].year >= year, axis=1).astype('int')
>>> dfy
   mem_no   status  animal       join        end  mem_2002  mem_2003  mem_2004  mem_2005  mem_2006  mem_2007  mem_2008  mem_2009
0       4   Active    Lark 2002-12-02 2007-10-23         1         1         1         1         1         1         0         0
1     101  Expired  Parrot 2003-04-01 2016-02-01         0         1         1         1         1         1         1         1
2     118  Crunchy    Frog 2001-10-08 2002-02-22         1         0         0         0         0         0         0         0
3     121   Grumpy   Panda 2003-05-01 2004-03-01         0         1         1         0         0         0         0         0

关于python - Pandas:识别范围内值的重叠成员资格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40951073/

相关文章:

python - Django 时区字段不接受所有 pytz 时区

python - 如何使用卡方检验从文档中计算关键术语?

python - 如何在 pandas 数据框中有效地存储分数列表和按日期分组

python - 如何在 Pandas 数据框的每一行的开头添加动态数量的空格?

pandas - 在 pandas 数据帧列上使用数学包(例如 math.radians、math.tan)

Python 列表函数

python - Mirametrix S2 凝视追踪器 : Sending general purpose input (GPI) values always fails

python - RegExp 从 Redmine 日志文件中提取数据

Python Pandas 数据框查找缺失值

python pandas 分组依据和聚合列