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

1
  • 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: stackoverflow.com/questions/26483254/… Commented May 20, 2019 at 10:24

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

@TrinhPham You're welcome! Also, next time, please refrain from posting further clarifications as answers - edit your question or post comments instead.
Can you explain your code ? Because I want understand this code . Thank @gmds
@TrinhPham Is my explanation in the answer unclear?
Yes , I have learned Python a week ago , So I hope you explain more clearly
In that case, there would be too much to explain. I suggest you start by reading the datetime module documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.