繁体   English   中英

如何从两个形状相同的Pandas数据框中选择元素位置,且值在一定范围内匹配?

[英]How to select element locations from two Pandas dataframes of identical shape, where the values match within a certain range?

如何从两个形状相同的Pandas数据框中选择元素位置,且值在一定范围内匹配? 执行此操作的代码可能很容易编写,但是我想知道是否存在使用Pandas数据帧进行此条件选择(例如loc)的明智方法,因为我将需要大图像文件使用它,而且我相信Pandas通常是快速高效。

我需要有关的更多信息

他的值在一定范围内匹配

但这是选择两个DataFrame相同值的示例。 通过用任何其他测试替换该测试,您可以实现目标。

# Test data
df1 = DataFrame({'col1':[1.2, 3.2, 4.2], 'col2':[0, 2.1, 4.8], 'col3': [2.0, 0, 8.2]})
df2 = DataFrame({'col1':[2.2, 3.2, 4.2], 'col2':[4.1, 0, 4.8], 'col3': [2.0, 4.7, 8.2]})

# df1
#    col1  col2  col3
# 0   1.2   0.0   2.0
# 1   3.2   2.1   0.0
# 2   4.2   4.8   8.2

# df2
#    col1  col2  col3
# 0   2.2   4.1   2.0
# 1   3.2   0.0   4.7
# 2   4.2   4.8   8.2

# Assuming the two DataFrame have the same index and columns you can simply do that

df2[df2 == df1]

#    col1  col2  col3
# 0   NaN   NaN   2.0
# 1   3.2   NaN   NaN
# 2   4.2   4.8   8.2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM