Ritu Singh
Problem:
I have a data frame in Python Pandas, like below:
The column my_date is in datetime format.
In my real data frame, I have many more columns.
And I need to create a new column named col_x where for each id will be values 1,2,3 and so on based on date in column my_date. So, for each id in column col_x has to be 1 in the early date from my_date and so on.
So, as a result, I need something like below:
How can I do that in Python Pandas?
Solution:
Looks like you could use a >groupby.rank:
This is more or less equivalent to finding the sorting order ((e.g.,using >numpy.argsort, or rank('first')), except it will rank the duplicate values equally:
Output:
Example with duplicate values:
Suggested blogs:
>Invoking Python script from scons and pass ARGLIST
>Plugins and Presets for Vuejs project
>Python Error Solved: load_associated_files do not load a txt file
>PHP cURL to upload video to azure blob storage
>PHP Error Solved: htaccess problem with an empty string in URL
>Python Error Solved: pg_config executable not found
>Set up Node.js & connect to a MongoDB Database Using Node.js
>Setting up a Cloud Composer environment: Step-by-step guide>Migrate From Haruko To AWS App: 5 Simple Steps