Python - take the time difference from the first date in a column

2024/10/12 20:25:25

Given the date column, I want to create another column diff that count how many days apart from the first date.

date                    diff
2011-01-01 00:00:10      0
2011-01-01 00:00:11      0.000011 days
2011-02-01 00:00:11      30.000011 days 
2013-02-01 00:00:11      395.000011 days
2014-02-01 00:00:11      760.000011 days

Dates are in datetime. What I tried so far:

df = df.sort_values(['date'], ascending=True)
df.set_index('date', inplace = True)
first = df.index[0]
df['diff'] = (first - df.index.shift()).fillna(0)
Answer

you can try

df['diff'] = df.date - df.date.min()dfdate               diff
0 2011-01-01 00:00:10    0 days 00:00:00
1 2011-01-01 00:00:11    0 days 00:00:01
2 2011-02-01 00:00:11   31 days 00:00:01
3 2013-02-01 00:00:11  762 days 00:00:01
4 2014-02-01 00:00:11 1127 days 00:00:01
https://en.xdnf.cn/q/118163.html

Related Q&A

(Django) Limited ForeignKey choices by Current User

Update Thanks to Michael I was able to get this to work perfectly in my CreateView, but not in the UpdateView. When I try to set a form_class it spits out an improperly configured error. How can I go a…

Parse a custom text file in Python

I have a text to be parsed, this is a concise form of the text.apple {type=fruitvarieties {color=redorigin=usa} }the output should be as shown belowapple.type=fruit apple.varieties.color=red apple.vari…

Logs Dont Overwrite

Im using Pythons logging.config module to configure and use a logging tool in my project.I want my log files to overwrite each time (not append), so I set my YAML configuration file like this:# logging…

How to upload local files to Firebase storage from Jupyter Notebook using Python

Since I guess importing google.cloud.storage might be a very first step to set API connecting the firebase storage, what I did first is to install google-cloud on Ubuntu like this:$ pip install --upgra…

How can scrapy crawl more urls?

as we see:def parse(self, response):hxs = HtmlXPathSelector(response)sites = hxs.select(//ul/li)items = []for site in sites:item = Website()item[name] = site.select(a/text()).extract()item[url] = site.…

Pyplot - shift position of y-axis ticks and its data

Using pyplot, how do I modify my plot to change the vertical position of my yticks? E.g. in my plot above, I want to move Promoter down and CDS up (along with their lines in the plot).For the above pl…

How to exit a Python program or loop via keybind or macro? Keyboardinterrupt not working

I am trying to complete a simple GUI automation program that merely opens a web page and then clicks on a specific spot on the page every 0.2 seconds until I tell it to stop. I want my code to run and …

SKlearn prediction on test dataset with different shape from training dataset shape

Im new to ML and would be grateful for any assistance provided. Ive run a linear regression prediction using test set A and training set A. I saved the linear regression model and would now like to use…

How to eliminate suspicious barcode (like 123456) data [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 6…

how to get href link from onclick function in python

I want to get href link of website form onclick function Here is html code in which onclick function call a website <div class="fl"><span class="taLnk" onclick="ta.tr…