[英]how to join two dataframes for which column values are within a certain range for multiple columns using pandas dataframe?
[英]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.