首页 > 解决方案 > 如何使用if条件合并两个数据框

问题描述

数据框_1:df3

事件ID 邮政编码 Event_Start_Date Event_End_Date
234 112223 2015-09-17 2015-09-30
234 112233 2015-09-30 2015-10-07
234 112323 2015-09-25 2015-10-05
456 331212 2001-06-17 2001-06-18
456 332211 2001-06-12 2001-06-12
456 332212 2001-06-14 2001-06-15
789 223321 1990-08-16 1990-08-16
789 223322 1990-08-12 1990-08-14
789 223324 1990-08-22 1990-08-22

Dateframe_2:df_NA

索赔号 邮政编码 报告日期 事件ID
WC8285054 112233 2015-09-24
WC6982237 112323 2015-09-27
WC6982242 112323 2015-09-08
WC6982243 112323 2015-09-07
WC6982248 223324 1990-08-1

如果 Dateframe_2 中的邮政编码等于 Dataframe_1 中的邮政编码

and  ReportDate in Dataframe_2 lies between Event_Start_Date and Event_End_Date than 

    than EventID in Dataframe_2 is same as EventID in Dateframe_1.

我的代码给出了值错误:

for ClaimNumber, Postcode, ReportDate, EventID, *_ in df_NA:

    for EventID, Postcode, Event_Start_Date, Event_End_Date, *_ in df3:

        if df_NA['Postcode']==df3['Postcode'] and df_NA['ReportDate']>= df3['Event_Start_Date'] and df_NA['ReportDate']<= df3['Event_End_Date']:

            df_NA['EventID']=df['EventID']




ValueError                                Traceback (most recent call last)
<ipython-input-21-e7f9a0046929> in <module>
      1 for Postcode, ReportDate, EventID, *_ in df_NA:
      2     for EventID, Postcode, Event_Start_Date, Event_End_Date, *_ in df3:
----> 3         if df_NA['Postcode']==df3['Postcode'] and df_NA['ReportDate']>= df3['Event_Start_Date'] and df_NA['ReportDate']<= df3['Event_End_Date']:
      4             df_NA['EventID']=df['EventID']
      5 

D:\anaconda\lib\site-packages\pandas\core\ops\common.py in new_method(self, other)
     63         other = item_from_zerodim(other)
     64 
---> 65         return method(self, other)
     66 
     67     return new_method

D:\anaconda\lib\site-packages\pandas\core\arraylike.py in __eq__(self, other)
     27     @unpack_zerodim_and_defer("__eq__")
     28     def __eq__(self, other):
---> 29         return self._cmp_method(other, operator.eq)
     30 
     31     @unpack_zerodim_and_defer("__ne__")

D:\anaconda\lib\site-packages\pandas\core\series.py in _cmp_method(self, other, op)
   4971 
   4972         if isinstance(other, Series) and not self._indexed_same(other):
-> 4973             raise ValueError("Can only compare identically-labeled Series objects")
   4974 
   4975         lvalues = extract_array(self, extract_numpy=True)

ValueError: Can only compare identically-labeled Series objects

标签: pythonpandas

解决方案


推荐阅读