-1

Hi I am using the date difference as a machine learning feature, analyzing how the weight of a patient changed over time.

enter image description here

I successfully tested a method to do that as shown below, but the question is how to extend this to a dataframe where I have to see date difference for each patient as shown in the figure above. The encircled column is what im aiming to get. So basically the baseline date from which the date difference is calculated changes every time for a new patient name so that we can track the weight progress over time for that patient! Thanks

s='17/6/2016'
s1='22/6/16'
a=pd.to_datetime(s,infer_datetime_format=True)
b=pd.to_datetime(s1,infer_datetime_format=True)
e=b.date()-a.date()
str(e)
str(e)[0:2]

I think it would be something like this, (but im not sure how to do this exactly):

def f(row):
    # some logic here 

    return val

df['Datediff'] = df.apply(f, axis=1)
Ray92
  • 439
  • 6
  • 18
  • Can you post the intended dataframe? – Ananay Mital Jan 25 '19 at 18:06
  • Hi I edited the question to make it more clear, thanks! – Ray92 Jan 25 '19 at 22:34
  • Possible duplicate of [How to calculate day's difference between successive pandas dataframe rows with condition](https://stackoverflow.com/questions/35534152/how-to-calculate-days-difference-between-successive-pandas-dataframe-rows-with) – Ananay Mital Jan 26 '19 at 05:20

2 Answers2

2

You can use transform with first

 df['Datediff'] = df['Date'] - df1.groupby('Name')['Date'].transform('first')

Another solution can be using cumsum

 df['Datediff'] = df.groupby('Name')['Date'].apply(lambda x:x.diff().cumsum().fillna(0))
No_body
  • 832
  • 6
  • 21
1
df["Datediff"] = df.groupby("Name")["Date"].diff().fillna(0)/ np.timedelta64(1, 'D')
df["Datediff"]

0      0.0
1     12.0
2     14.0
3     66.0
4     23.0
5      0.0
6     10.0
7     15.0
8     14.0
9      0.0
10    14.0
Name: Datediff, dtype: float64
Ananay Mital
  • 1,395
  • 1
  • 11
  • 16
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value – Andrew Jan 26 '19 at 07:31