如何在值上比较两个Pandas DataFrame?

时间:2018-02-11 23:07:07

标签: python pandas dataframe

我有两个Pandas DataFrames(A& B),纬度和经度。

我需要对它们进行比较,如果DF A中存在来自DF B的纬度和经度,则附加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

如何实现预期的输出(见下文)?

 Long lat               | Result
--------------------------------
-37.3794288,175.6697856 | False
-37.0334148,174.8680204 | True
-41.173852,174.981931   | False

2 个答案:

答案 0 :(得分:2)

这是一种方式:

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

或者,更多的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})

答案 1 :(得分:1)

我不确定这是多么有效,但您可以使用多索引

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