Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9259433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:39:25+00:00 2026-06-18T12:39:25+00:00

I have a pandas DataFrame containing timestamped events from multiple users. By default, the

  • 0

I have a pandas DataFrame containing timestamped events from multiple users. By default, the DataFrame is sorted by timestamp.

uid timestamp other_vars
  1       100        ...
  1       150        ...
  2       150        ...
  2       200        ...
  1       225        ...
  3       300        ...
  3       400        ...

I’d like to get the diff of the timestamp within users. That is, for each event, I want to get the time elapsed since the previous event generated by the same user.

uid timestamp diff other_vars
  1       100   NA        ...
  1       150   50        ...
  2       150   NA        ...
  2       200   50        ...
  1       225   75        ...
  3       300   NA        ...
  3       400  100        ...

Is there a clean way to do this in pandas, ideally without sorting by User? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T12:39:26+00:00Added an answer on June 18, 2026 at 12:39 pm

    As mentioned in the comments, you can use groupby. I’d groupby and then diff. groupby will (unsurprisingly) group the rows:

    >>> df
       uid  timestamp other_vars
    0    1        100        ...
    1    1        150        ...
    2    2        150        ...
    3    2        200        ...
    4    1        225        ...
    5    3        300        ...
    6    3        400        ...
    >>> for name, gr in df.groupby("uid"):
    ...     print name
    ...     print gr
    ...     
    1
       uid  timestamp other_vars
    0    1        100        ...
    1    1        150        ...
    4    1        225        ...
    2
       uid  timestamp other_vars
    2    2        150        ...
    3    2        200        ...
    3
       uid  timestamp other_vars
    5    3        300        ...
    6    3        400        ...
    

    And then we select the column we’re interested in along these groups and diff it:

    >>> df["diff"] = df.groupby("uid")["timestamp"].diff()
    >>> df
       uid  timestamp other_vars  diff
    0    1        100        ...   NaN
    1    1        150        ...    50
    2    2        150        ...   NaN
    3    2        200        ...    50
    4    1        225        ...    75
    5    3        300        ...   NaN
    6    3        400        ...   100
    

    Note that we didn’t sort the timestamps, so if you wanted that you’ve have to do it explicitly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a pandas DataFrame containing timestamped events. Each event has a start time
I have the following pandas Dataframe: from pandas import DataFrame, MultiIndex index = MultiIndex.from_tuples(zip([21,22,23],[45,45,46]),
I have a pandas dataframe consisting of 23 series with a default sequential index
I have two pandas dataframes: from pandas import DataFrame df1 = DataFrame({'col1':[1,2],'col2':[3,4]}) df2 =
I have a list of stockmarket data pulled from Yahoo in a pandas DataFrame
I have a pandas DataFrame and I want to delete rows from it where
I have a pandas DataFrame that has multiple columns in it: Index: 239897 entries,
I am researching/backtesting a trading system. I have a Pandas dataframe containing OHLC data
I have pandas DataFrame. I would like to get single value from a column
I have a pandas DataFrame with a date column. It is not an index.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.