如何使用if条件合并两个数据帧

2024-05-08 13:09:24 发布

您现在位置:Python中文网/ 问答频道 /正文

数据帧_1:df3

^{tb1}$

日期框2:df\u NA

^{tb2}$

如果日期框_2中的邮政编码等于数据框_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.

我的代码提供了valueserrors:

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

Tags: andinselfeventdfdatestartmethod