in Education by
I have a dataframe that looks like this: from to datetime other ------------------------------------------------- 11 1 2016-11-06 22:00:00 - 11 1 2016-11-06 20:00:00 - 11 1 2016-11-06 15:45:00 - 11 12 2016-11-06 15:00:00 - 11 1 2016-11-06 12:00:00 - 11 18 2016-11-05 10:00:00 - 11 12 2016-11-05 10:00:00 - 12 1 2016-10-05 10:00:59 - 12 3 2016-09-06 10:00:34 - I want to groupby "from" and then "to" columns and then sort the "datetime" in descending order and then finally want to calculate the time difference within these grouped by objects between the current time and the next time. For eg, in this case, I would like to have a dataframe like the following: from to time diff in minutes 11 1 120 11 1 255 11 1 225 11 1 0 11 12 300 11 12 0 11 18 0 12 1 25 12 3 0 I can't get my head around figuring this out!! Is there a way out for this? Any help will be much much appreciated!! Thank you so much in advance! Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
You can use the following functions in the Pandas library to solve this problem: DataFrame.assign It assigns new columns to a DataFrame, returning a new object with all the original columns in addition to the new ones. DataFrame.sort_values Sort by the values along column or row. For example: df.assign( timediff=df.sort_values( 'datetime', ascending=False ).groupby(['from', 'to']).datetime.diff(-1).dt.seconds.div(60).fillna(0)) Output: Hope this answer helps. If you want to be build successful data science career then enroll for best data science certification.

Related questions

0 votes
    I'm looking for a decent implementation of the OPTICS algorithm in Python. I will use it to form density-based ... to that cluster. Select the correct answer from above options...
asked Jan 28, 2022 in Education by JackTerrance
0 votes
    libsvm and liblinear are both software libraries that implement Support Vector Machines. What's the difference? And ... than libsvm? Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    In this video from Sebastian Thrun, he says that supervised learning works with "labeled" data and unsupervised ... basic difference. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I need to know the most efficient way of installing JQ on Mac (el capitan). The code is downloaded to my mac ... via the command line. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    Suppose I have a Tensorflow tensor. How do I get the dimensions (shape) of the tensor as integer values? I ... 'Dimension' instead. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I'm hoping to use either Haskell or OCaml on a new project because R is too slow. I need to be able to ... in either Haskell or OCaml? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I'm hoping to use either Haskell or OCaml on a new project because R is too slow. I need to be able to ... in either Haskell or OCaml? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    I'm Working on document classification tasks in java. Both algorithms came highly recommended, what are the ... Processing tasks? Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    I am trying to decompose a 3D matrix using python library scikit-tensor. I managed to decompose my Tensor ... the decomposed matrices? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I am trying to do a hyperparameter search using scikit-learn's GridSearchCV on XGBoost. During grid search ... ':42} Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    What is the difference between the two? It seems that both create new columns, in which their number is equal to ... they are in. Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I am receiving the error: ValueError: Wrong number of items passed 3, placement implies 1, and I am struggling to ... 'sigma'] = sigma Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    I am training on 970 samples and validating on 243 samples. How big should batch size and number of epochs be ... on data input size? Select the correct answer from above options...
asked Feb 1, 2022 in Education by JackTerrance
0 votes
    If I want to use the BatchNormalization function in Keras, then do I need to call it once only at the ... much of a difference. Select the correct answer from above options...
asked Jan 31, 2022 in Education by JackTerrance
0 votes
    I have learned a Machine Learning course using Matlab as a prototyping tool. Since I got addicted to F#, I ... of resources? Thanks. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
...