python - Python 计数转换并将日期聚合到月份列
问题描述
我有一个数据集,我试图在其中分解并按月计算出现次数。我还需要转换最终结果以添加每个月/发生的列
Report_ID Report_name timestamp
1 Profit 8/1/2018 06:10:40
2 Revenue 8/5/2018 09:25:45
1 Profit 8/29/2018 10:11:30
2 Revenue 9/1/2018 09:45:22
输出:
Report_ID 8/2018 9/2018
1 2 0
2 1 1
解决方案
pd.crosstab
与 dt 访问器一起使用strftime
:
pd.crosstab(df.Report_ID, df['timestamp'].dt.strftime('%m/%Y'))\
.reset_index()\
.rename_axis([None], axis=1)
输出:
Report_ID 08/2018 09/2018
0 1 2 0
1 2 1 1
推荐阅读
- javascript - Pusher 没有捕捉到触发的事件,因为从 2018 年 9 月 6 日开始,Laravel 中的 TLS 发生了变化
- r - 使用带有 httr 包的 R 对 Tradier 的 API 调用问题
- wordpress - NGINX+Wordpress 缓存悖论
- c# - 如何仅使用单个输入端点将数据写入 Android 中的 USB HID 设备
- mongodb - 在 mongodb 中查找包含任何类型子字段的所有记录
- javascript - 将所选项目附加到列表
- scala - Scala:获取对象类名的正确方法?
- jquery - 3 列带图像 - 在桌面上底部对齐但在调整大小时不对齐
- postgresql - 如何更改 postgresql 中事件触发器的所有者?
- c - 正确使用 Callback 进行相同操作和不同类型