我有一个包含日期的数据框,我想为特征工程处理如下数据
测向
date
2016/1/1
2015/2/10
2016/4/5
之后的过程,我想使df看起来像
date Jan Feb Mar Apr
2016/1/1 30 0 0 0 //date from 1/1 to 1/30 : the number of dates in jan
2015/2/10 0 19 11 0 //date from 2/10 to 3/11 : the number of dates in feb and no of dates in mar
2016/3/25 0 0 7 21 //date from 3/25 to 4/21 : the number of dates in mar and no of dates in apr
在df[“date”]之后30天
df[“日期”]+时间差(月=1)
计算属于特定30天的月份频率
有没有什么方法可以快速做到这一点?你知道吗
谢谢。你知道吗
一步一步走。首先,将原始日期偏移
+ pd.to_timedelta('30d')
。然后创建一个列,用df.date.dt.month
表示月份。然后为每个日期创建一个带有月末日期的列—这里有一些想法:Want the last day of each month for a data frame in pandas。最后,填写一个矩阵,其中列是12个月,在列中设置月份和月份+1的值。你知道吗通过一次充实一列数据帧,您可以轻松地从输入移动到所需的输出。不太可能有一个神奇的方法在一个调用中完成所有事情。你知道吗
阅读这里的所有关于日期/时间函数:https://pandas.pydata.org/pandas-docs/stable/timeseries.html-有很多!你知道吗
可以将自定义函数与^{} 一起使用,将
groupby
与size
一起使用:与^{} 类似的解决方案:
另一种解决方案:
相关问题 更多 >
编程相关推荐