0
我有一個帶Date1和Date2列的SFrame。在Sframes中使用.apply()來操縱每一行的多列
我想使用.apply()
找到Date1和Date2之間的datediff,但我無法弄清楚如何使用另一個參數。
理想像
frame['new_col'] = frame['Date1'].apply(lambda x: datediff(x,frame('Date2')))
我有一個帶Date1和Date2列的SFrame。在Sframes中使用.apply()來操縱每一行的多列
我想使用.apply()
找到Date1和Date2之間的datediff,但我無法弄清楚如何使用另一個參數。
理想像
frame['new_col'] = frame['Date1'].apply(lambda x: datediff(x,frame('Date2')))
您可以直接從frame['Date2']
只是減去frame['Date1']
採取Date2
列和那些在Date1
的日期之間的差值。也就是說,由於某種原因,返回秒的兩個日期之間的數(僅適用於Python的datetime
對象測試),它可以轉換成數與簡單的算術天的:
from sframe import SFrame
from datetime import datetime, timedelta
mydict = {'Date1':[datetime.now(), datetime.now()+timedelta(2)],
'Date2':[datetime.now()+timedelta(10), datetime.now()+timedelta(17)]}
frame = SFrame(mydict)
frame['new_col'] = (frame['Date2'] - frame['Date1']).apply(lambda x: x//(60*60*24))
輸出:
+----------------------------+----------------------------+---------+
| Date1 | Date2 | new_col |
+----------------------------+----------------------------+---------+
| 2016-10-02 21:12:14.712556 | 2016-10-12 21:12:14.712574 | 10.0 |
| 2016-10-04 21:12:14.712567 | 2016-10-19 21:12:14.712576 | 15.0 |
+----------------------------+----------------------------+---------+
謝謝!我知道這聽起來很愚蠢,但這並沒有出現在我身上...... – javsckc
所以你期待在天有所不同? – sb0709
是的。但是試圖避免迭代,而是使用.apply()函數。 – javsckc
'frame ['Date1'] - frame ['Date2']'不起作用? – Abdou