Python使用日期数据减去两列,从csv中获取周数,月数?

时间:2018-02-14 05:40:48

标签: python-3.x pandas

我有一个csv,其中我有两列代表开始日期:st_dt和结束日期:'end_dt`,我必须减去这些列以获得周数。我尝试使用pandas迭代列,但似乎我的输出是错误的。

 st_dt                 end_dt
---------------------------------------
20100315           20100431

1 个答案:

答案 0 :(得分:2)

read_csvconst arr = {[1]: [{id: 1, category: 1}, {id: 2, category: 2}], [2]: [{id: 3, category: 2}, {id: 4, category: 2}], [3]: [{id: 5, category: 3}, {id: 6, category: 3}]}, result = [].concat(...Object.values(arr)).reduce((r,{id, category}) => { r[category] = r[category] || []; r[category].push({id, category}); return r; },{}); console.log(result);一起用于日期时间,然后在减去days之后:

parse_dates

如果某些日期错误,例如df = pd.read_csv(file, parse_dates=[0,1]) print (df) st_dt end_dt 0 2010-03-15 2010-04-30 df['diff'] = (df['end_dt'] - df['st_dt']).dt.days print (df) st_dt end_dt diff 0 2010-03-15 2010-04-30 46 使用带有参数20100431的{​​{3}}将其转换为errors='coerce'

NaT