0

I have a problem. I want to calculate the difference between two days. Unfourntaly I got x days as result. How could I get only the days without the word days?

Dataframe

   customerId    fromDate  number
0           1  2021-02-22       1
1           1  2021-03-18       1
2           1  2021-03-22       1
3           1  2021-02-10       1
4           1  2021-09-07       1
5           1        None      98
6           1  2022-01-18       1
7           2  2021-05-17     102
from datetime import date
import pandas as pd


d = {'customerId': [1, 1, 1, 1, 1, 1, 1, 2],
     'fromDate': ['2021-02-22', '2021-03-18', '2021-03-22', '2021-02-10', '2021-09-07', None, '2022-01-18', '2021-05-17'],
      'number': [1, 1, 1, 1, 1, 98, 1, 102]
    }
    
df = pd.DataFrame(data=d)
display(df)

df['fromDate'] = pd.to_datetime(df['fromDate'], errors='coerce')
df_last = df.iloc[df.groupby('customerId').apply(lambda x: x['fromDate'].idxmax())].copy()
#display(df_last)
df_last['days'] = pd.to_datetime(date.today()) - df_last['fromDate']
display(df_last)

[OUT]
   customerId   fromDate  number     days
6           1 2022-01-18       1 126 days
7           2 2021-05-17     102 372 days

What I want

    customerId  fromDate    number  days
6   1           2022-01-18  1       126 
7   2           2021-05-17  102     372 
Test
  • 562
  • 13

0 Answers0