-1

Sorry if my question is simple i'm starting(so thank you for your help and understanding) I am trying to get a date discrepancy by 'identifier' A B C D in the DF example. Using Python how can i add a column to establish the delta between each contract knowing that a person can have only one contract as he can have 10 or more. Thank you in advance.

header 1 header 2
cell 1 cell 2
cell 3 cell 4

I have try many things by DSS and Python but my result is false....

YOUSSRA
  • 3
  • 1
  • Your question and your sample data are not correlated. You mention date discrepancy, yet your data doesn't show dates, you mention 'A', 'B', 'C', 'D', yet your sample data doesn't show these columns. – itprorh66 Nov 27 '22 at 14:43

1 Answers1

0

You mean something like:

df['new_col'] = df['header1'] - df['header2']

For timedeltas use:

import numpy as np
df['diff_days'] = (df['end_date'] - df['start_date']) / np.timedelta64(1, 'D')

D stands for timediffernce in days. Use "W", "M", "Y" for weeks, months or years.

drevil
  • 58
  • 6
  • Thank you for this answer, I will try timedelta tomorow but in my case I need to search by ID something like this: DF['new_col_diff'] = df['header 1 date end contract previous']- df['header2 date begin next contract'] by ID agent i don't knwo how to group by agent because it's a serie and all ID have one or many contracts perhaps Pivot for this ?? thank you for your help – YOUSSRA Nov 27 '22 at 17:06
  • Have you tried pandas groupby-function? https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html – drevil Nov 28 '22 at 07:16
  • Finaly i used SQL for take the first date contrat and it's ok. thank you for your answers. – YOUSSRA Dec 01 '22 at 08:40