【发布时间】:2015-12-15 22:27:23
【问题描述】:
我是 Python 新手,我正在尝试将我创建的功能组合在两个独立的程序中,这些程序对我有用。
目标是按各种描述对值进行分组,然后按日期对数据集的平均值进行分组。我已经使用 Pandas Groupby 成功完成了这项工作。
我想评估的描述之一是在数据集中每个点的给定距离内进行平均。到目前为止,我一直在使用邮政编码作为位置描述来近似这一点。另外,我已经能够使用 Geopy 来确定数据集中使用 GPS 点在所需距离内的所有其他点。这为我提供了所需距离内数据集中每个 ID 的 ID 列表。
这是一个示例数据集:
ID Date Value Color Location
1 1 1234 Red 60941
1 2 51461 Red 60941
1 3 6512 Red 60941
1 4 5123 Red 60941
1 5 48413 Red 60941
2 1 5416 Blue 60941
2 2 32 Blue 60941
2 3 18941 Blue 60941
2 4 5135 Blue 60941
2 5 1238 Blue 60941
3 1 651651 Blue 60450
3 2 1777 Blue 60450
3 3 1651 Blue 60450
3 4 1968 Blue 60450
3 5 846 Blue 60450
4 1 1689 Red 60941
4 2 1651 Red 60941
4 3 184 Red 60941
4 4 19813 Red 60941
4 5 132 Red 60941
5 1 354 Yellow 60450
5 2 684 Yellow 60450
5 3 489 Yellow 60450
5 4 354 Yellow 60450
5 5 846 Yellow 60450
这是我目前使用邮政编码位置描述的 Pandas 代码:
average_df = data_df['Value'].groupby([data_df['Location'],data_df['Color'],data_df['Date']]).mean()
有没有办法将从 Geopy 获得的列表传递给 Groupby 来代替我目前拥有的 ['Location'] 组?例如,Groupby List(ID) [List 1: (1,2,3), List 2: (3,1,5), List 3:(2,3,4)] 然后是颜色和日期。
我浏览了 Pandas 文档并搜索了这个网站,但没有找到任何人使用 Pandas Groupby 中的列表,所以我不确定这是否可行。也许我需要在一个 numpy 数组中执行此操作?任何反馈将不胜感激。
【问题讨论】:
-
想必您已经在运行后将 Geopy 进程中的数据添加到了数据框中。即使数据驻留在框架中,是否有理由在用新列替换“位置”之前不能调用 Groupby?
标签: python list numpy pandas group-by