как сравнить две даты, повторяя во фрейме данных pandas и создавая новый столбец

У меня есть фрейм данных pandas с транзакциями клиентов, как показано ниже, и я создаю столбец с именем «Метка» с двумя разными значениями.

  • Новая транзакция, выполненная до даты окончания предыдущей транзакции

  • Новая транзакция, выполненная после даты окончания предыдущей транзакции

Ввод

Transaction ID    Transaction Start Date  Transaction End Date 

      1               23-jun-2014              15-Jul-2014

      2               14-jul-2014              8-Aug-2014        

      3               13-Aug-2014              22-Aug-2014        

      4               21-Aug-2014              28-Aug-2014      

      5               29-Aug-2014              05-Sep-2014

      6               06-Sep-2014              15-Sep-2014

Желаемый результат

Transaction ID    Transaction Start Date  Transaction End Date  Label

  1               23-jun-2014              15-Jul-2014

  2               14-jul-2014              8-Aug-2014       New Transaction performed before end date of previous transaction

  3               13-Aug-2014              22-Aug-2014      New Transaction after the end date of previous transaction.    

  4               21-Aug-2014              28-Aug-2014      New Transaction performed before the end date of previous transaction.

  5               29-Aug-2014              05-Sep-2014      New Transaction after the end date of previous transaction.

  6               06-Sep-2014              15-Sep-2014      New Transaction after the end date of previous transaction.

person aeapen    schedule 26.02.2021    source источник


Ответы (2)


Используйте numpy.where и _ 2_:

import numpy as np

df['Label'] = np.where(df['Transaction Start Date'].lt(df['Transaction End Date'].shift()), 'New Transaction performed before end date of previous transaction', 'New Transaction after the end date of previous transaction.')
person Mayank Porwal    schedule 26.02.2021

Сначала используйте to_datetime, затем _ 2_ с _ 3_ формируют менее скомпилированные сдвинутые значения на _ 4_ и последним первым значением будет пустая строка:

df['Transaction End Date'] = pd.to_datetime(df['Transaction End Date'])
df['Transaction Start Date'] = pd.to_datetime(df['Transaction Start Date'])

df['Label'] = np.where(df['Transaction Start Date'].lt(df['Transaction End Date'].shift()), 
                       'New Transaction performed before end date of previous transaction', 
                       'New Transaction after the end date of previous transaction.')
df.loc[0, 'Label'] = ''

Альтернативное решение:

m = df['Transaction Start Date'].lt(df['Transaction End Date'].shift())

df['Label'] = [''] + np.where(m, 
              'New Transaction performed before end date of previous transaction', 
              'New Transaction after the end date of previous transaction.')[1:].tolist()

print (df)
   Transaction ID Transaction Start Date Transaction End Date  \
0               1             2014-06-23           2014-07-15   
1               2             2014-07-14           2014-08-08   
2               3             2014-08-13           2014-08-22   
3               4             2014-08-21           2014-08-28   
4               5             2014-08-29           2014-09-05   
5               6             2014-09-06           2014-09-15   

                                               Label  
                                                     
1  New Transaction performed before end date of p...  
2  New Transaction after the end date of previous...  
3  New Transaction performed before end date of p...  
4  New Transaction after the end date of previous...  
5  New Transaction after the end date of previous...  
person jezrael    schedule 26.02.2021
comment
Я не думаю, что в этом есть необходимость groupby. Транзакции выглядят последовательными. - person Mayank Porwal; 26.02.2021
comment
@MayankPorwal - ой, согласен. измененный. - person jezrael; 26.02.2021