python-2.7 - Geopandas 数据框/矩阵 - 过滤/删除 NaN/False 值

标签 python-2.7 numpy matrix pandas geopandas

我将 GeoSeries.almost_equals(other[,decimal=6]) 函数应用于具有 1000 万个条目的地理数据帧,以便找到彼此靠近的多个地理点。 : my data

enter image description here

这给了我矩阵,现在我需要过滤所有 True 值,以便仅使用与地理相关的 POI 创建 DF/列表,所以我使用了: enter image description here

现在,我很难弄清楚如何进一步处理这个矩阵的过滤器。 预期输出是向量、列表或理想情况下的 DF,具有所有 TRUE(匹配)值,但彼此匹配为 1 到 1,并且重复(如果 [1,9] 则 [9,1] 从输出中删除 列表示例: enter image description here

DF 示例:

enter image description here

最佳答案

考虑这个示例数据框:

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/

相关文章:

java - python 中的索引错误

python - python 中 csv writer 的输出不一致

python - 使用掩蔽数组的一行计算数组行的平均值

python - python 中的矩阵

c++ - 使用 Bcast、Scatter 和 Gather 以及动态分配时出现 MPI 段错误

Python - 如何使所有按钮每行对齐,看起来像按钮墙

Python打印对象设计

python - 用 Python 从 3D 到 2D

python - TypeError : The value of a feed cannot be a tf. Tensor对象传递一个热编码时

python - Python 中的 4x4 矩阵