我将 GeoSeries.almost_equals(other[,decimal=6]) 函数应用于具有 1000 万个条目的地理数据帧,以便找到彼此靠近的多个地理点。 :
这给了我矩阵,现在我需要过滤所有 True 值,以便仅使用与地理相关的 POI 创建 DF/列表,所以我使用了:
现在,我很难弄清楚如何进一步处理这个矩阵的过滤器。 预期输出是向量、列表或理想情况下的 DF,具有所有 TRUE(匹配)值,但彼此匹配为 1 到 1,并且重复(如果 [1,9] 则 [9,1] 从输出中删除 列表示例:
DF 示例:
最佳答案
考虑这个示例数据框:
In [1]: df = pd.DataFrame([[True, False, False, True],
...: [False, True, True, False],
...: [False, True, True, False],
...: [True, False, False, True]])
In [2]: df
Out[2]:
0 1 2 3
0 True False False True
1 False True True False
2 False True True False
3 True False False True
获取匹配索引的数据帧的可能解决方案:
首先,我使用 np.triu
只考虑上三角形(这样就不会出现重复项):
In [15]: df2 = pd.DataFrame(np.triu(df))
In [16]: df2
Out[16]:
0 1 2 3
0 True False False True
1 False True True False
2 False False True False
3 False False False True
然后我堆叠数据帧,为索引级别指定所需的名称,并仅选择具有“True”值的行:
In [17]: result = df2.stack()
In [18]: result
Out[18]:
0 0 True
1 False
2 False
3 True
1 0 False
1 True
2 True
3 False
2 0 False
1 False
2 True
3 False
3 0 False
1 False
2 False
3 True
dtype: bool
In [21]: result.index.names = ['POI_id', 'matched_POI_ids']
In [23]: result[result].reset_index()
Out[23]:
POI_id matched_POI_ids 0
0 0 0 True
1 0 3 True
2 1 1 True
3 1 2 True
4 2 2 True
5 3 3 True
然后您当然可以删除值为 true 的列: .drop(0, axis=1)
关于python-2.7 - Geopandas 数据框/矩阵 - 过滤/删除 NaN/False 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33058007/