[英]How to compare two Pandas DataFrames on values?
I have two Pandas DataFrames ( A
& B
) with latitude and longitude. 我有两个具有经度和纬度的Pandas DataFrames( A
& B
)。
I need to compare them, and if latitude and longitude from DF A
is present in DF B
then append a 1
else 0
. 我需要对其进行比较,如果DF B
存在来自DF A
纬度和经度,请附加1
否则0
。
DF A
LatLong
-37.3794288,175.6697856
-37.0334148,174.8680204
-41.173852,174.981931
DF B
KBATMLongLat
-37.0334148,174.8680204
-37.5575605,175.1584622
-37.0334148,174.8680204
How can I achieve the expected output (see below)? 如何获得预期的输出(见下文)?
Long lat | Result
--------------------------------
-37.3794288,175.6697856 | False
-37.0334148,174.8680204 | True
-41.173852,174.981931 | False
This is one way: 这是一种方法:
import pandas as pd
df1 = pd.DataFrame([[-37.3794288,175.6697856],
[-37.0334148,174.8680204],
[-41.173852,174.981931]],
columns=['Long', 'Lat'])
df2 = pd.DataFrame([[-37.0334148,174.8680204],
[-37.5575605,175.1584622],
[-37.0334148,174.8680204]],
columns=['Long', 'Lat'])
df1['Result'] = [tuple(i) in set(map(tuple, df2.values)) for i in df1.values]
# Long Lat Result
# 0 -37.379429 175.669786 False
# 1 -37.033415 174.868020 True
# 2 -41.173852 174.981931 False
Alternatively, more pandonic: 另外,更泛泛:
df = pd.merge(df1, df2, indicator=True, how='left').\
drop_duplicates().rename(columns={'_merge': 'Result'})
df['Result'] = df['Result'].map({'left_only': False, 'both': True})
I'm not sure how efficient this is, but you could use a multi-index 我不确定这样做的效率如何,但是您可以使用多索引
df1 = df1.set_index(["Long","Lat"])
df2 = df2.set_index(["Long","Lat"])
df1["Result"] = df1.index.isin(df2.index)
df1 = df1.reset_index()
df1
Long Lat Result
0 -37.379429 175.669786 False
1 -37.033415 174.868020 True
2 -41.173852 174.981931 False
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.