I have a dataframe with a date column (not a datetime index). I want to create another columns whose value is today minus date from the date column. I can do this as a vectorized operation ts['days ago'] = dt.date.today() -ts['foo']
But this gives me a Timedelta object, while I want an int. Timedelta has an attribute .days
that returns me an int. But I cannot see how to vectorize this operation to make a "days ago as int" column as below, as the vector subtraction gives me a series, not a Timedelta.
rng = pd.date_range('6/1/2016', periods=10, freq='D')
ts = pd.Series(rng, index=range(10)).to_frame()
ts.columns = ['foo']
ts['days ago'] = dt.date.today() -ts['foo']
ts['days ago as int'] = "?"
print ts.ix[3]['days ago'].days
# doesn't work
ts['days ago as int'] = ts.ix[:]['days ago'].days
ts
.apply
获取天数属性可以将您的天数作为ints
ts["days ago int "] = ts['days ago'].apply(lambda x:x.days)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.