2
CustomerNumber           TransactionDate
1                 [ 12/3/2019 12/4/2019 12/17/2019 ]
2                 [ 1/4/2019 4/4/2019]
3                 [ 7/5/2019]
4                 [ 7/5/2019 7/7/2019 9/5/2019 9/15/2019 10/15/2019]

Hi , I have This DataFrame TransactionDate(MM/DD/YYYY), I want to calculate the distance between 2 days in each sequence. I want result :

CustomerNumber           TransactionDate
1                 [ 1 13 ]
2                 [ 3 ]
3                 [ 0 ]
4                 [ 2 60 10 30]

I am a beginner Data Analysis with Python. Pls help me ideal

Trinh Pham
  • 49
  • 7
  • Can you share the file from which you are reading the data? Please note, the list items in each row should be separated by ',' comma separator [ 1/4/2019, 4/4/2019], as if now the dates are separated by ' ' space [ 1/4/2019 4/4/2019] but it is not the python proper format. Please refer to this link: https://stackoverflow.com/questions/26483254/python-pandas-insert-list-into-a-cell – DataFramed May 20 '19 at 10:24

1 Answers1

1

We can use datetime.timedelta for this by converting each value in each row to a datetime.datetime, taking the difference of consecutive values and extracting the day value.

from datetime import datetime

date_format = '%m/%d/%Y'

def differencer(value):
    return [(datetime.strptime(second, date_format) - datetime.strptime(first, date_format)).days 
            for first, second in zip(value, value[1:])] or [0]

df['TransactionDate'].apply(differencer)

Output:

0            [1, 13]
1               [90]
2                [0]
3    [2, 60, 10, 30]
Name: TransactionDate, dtype: object

I realise it is slightly different from what is asked in the question, but I believe that is a mistake. I did add an or [0] to convert empty lists, though.

gmds
  • 19,325
  • 4
  • 32
  • 58
  • @TrinhPham You're welcome! Also, next time, please refrain from posting further clarifications as answers - edit your question or post comments instead. – gmds May 20 '19 at 11:34
  • Can you explain your code ? Because I want understand this code . Thank @gmds – Trinh Pham May 21 '19 at 06:57
  • @TrinhPham Is my explanation in the answer unclear? – gmds May 21 '19 at 07:30
  • Yes , I have learned Python a week ago , So I hope you explain more clearly – Trinh Pham May 21 '19 at 07:37
  • In that case, there would be too much to explain. I suggest you start by reading the [`datetime` module documentation](https://docs.python.org/3/library/datetime.html). – gmds May 21 '19 at 07:42