我想创建一个新列,它将显示两个日期之间的时间增量,如以下熊猫数据框所示:
>>> hg[['not inc','date']]
not inc date
0 False 2012-02-29 00:00:00
1 False 2012-03-16 00:00:00
2 False 2012-04-04 00:00:00
3 True 2012-05-08 00:00:00
4 False 2012-05-12 00:00:00
5 False 2012-05-26 00:00:00
6 False 2012-06-09 00:00:00
7 False 2012-10-13 00:00:00
8 False 2012-11-10 00:00:00
9 True 2013-03-19 00:00:00
10 False 2013-04-01 00:00:00
11 False 2013-04-25 00:00:00
12 False 2013-05-04 00:00:00
13 False 2013-05-18 00:00:00
14 False 2013-06-01 00:00:00
15 True 2013-08-20 00:00:00
16 False 2013-08-31 00:00:00
17 False 2013-09-21 00:00:00
18 False 2013-10-05 00:00:00
19 False 2013-10-19 00:00:00
20 False 2013-11-09 00:00:00
21 True 2014-01-21 00:00:00
22 False 2014-02-08 00:00:00
23 False 2014-02-22 00:00:00
24 False 2014-03-08 00:00:00
25 False 2014-03-29 00:00:00
26 False 2014-04-19 00:00:00
27 True 2014-07-21 00:00:00
28 True 2014-08-01 00:00:00
29 False 2014-08-09 00:00:00
30 False 2014-08-30 00:00:00
31 False 2014-09-13 00:00:00
32 True 2014-09-26 00:00:00
33 False 2014-10-04 00:00:00
34 True 2015-01-08 00:00:00
35 True 2015-01-20 00:00:00
36 False 2015-01-31 00:00:00
37 False 2015-02-14 00:00:00
我希望日期差从减去 2012-01-02 开始并成为一个 int。
这是我尝试过但没有成功的方法,因为 prevdate 不会更新到新行的日期,而是一直引用 datetime(2012,01,02) 的原始起始位置。我在数据框的行中使用 iterrows。
>>>for index, row in hg.iterrows():
prevdate = datetime(2012,01,02)
dsince = row['date']-prevdate
prevdate = row['date']
print dsince
结果(我也不知道如何将值修改为int):
58 days, 0:00:00
74 days, 0:00:00
93 days, 0:00:00
127 days, 0:00:00
131 days, 0:00:00
145 days, 0:00:00
159 days, 0:00:00
285 days, 0:00:00
313 days, 0:00:00
442 days, 0:00:00
455 days, 0:00:00
479 days, 0:00:00
488 days, 0:00:00
502 days, 0:00:00
516 days, 0:00:00
596 days, 0:00:00
607 days, 0:00:00
628 days, 0:00:00
642 days, 0:00:00
656 days, 0:00:00
677 days, 0:00:00
750 days, 0:00:00
768 days, 0:00:00
782 days, 0:00:00
796 days, 0:00:00
817 days, 0:00:00
838 days, 0:00:00
931 days, 0:00:00
942 days, 0:00:00
950 days, 0:00:00
971 days, 0:00:00
985 days, 0:00:00
998 days, 0:00:00
1006 days, 0:00:00
1102 days, 0:00:00
1114 days, 0:00:00
1125 days, 0:00:00
1139 days, 0:00:00
为了让事情变得更复杂一点,我只想创建另一列,其中行之间的日期差异对于“非公司”列具有 False 。
谢谢你。