Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Numbering rows in Pandas without order

I have a table Mapping that looks like this

Mapping
+---------+---------+
| user_id | prod_id |
+---------+---------+
|     41  |  N12ad  |
|      2  |  nd5Ed  |
|     73  |  bDe4d  |
|     25  |  M87he  |
+---------+---------+

I would like to add a column ranking that numbers the rows in the order that they are stored in the database. So that it looks like this

+---------+---------+---------+
| user_id | prod_id | ranking |
+---------+---------+---------+
|     41  |  N12ad  |      1  |
|      9  |  nd5Ed  |      2  |
|     73  |  bDe4d  |      3  |
|     25  |  M87he  |      4  |
+---------+---------+---------+

In Microsoft SQL, I can do

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT user_id, prod_id, ROW_NUMBER() OVER(PARTITION BY (SELECT NULL)) AS ranking
FROM Mapping

What would be the pandas equivalent of ROW_NUMBER() OVER(PARTITION BY (SELECT NULL))? Thank you for your help.

>Solution :

You can do this by adding a new column using range and the length of the DataFrame. The equivalent of ROW_NUMBER() with no partitioning is simply assigning row numbers in the order of the rows.

data = {
    "user_id": [41, 9, 73, 25],
    "prod_id": ["N12ad", "nd5Ed", "bDe4d", "M87he"]
}
df = pd.DataFrame(data)

# Adding the the ranking column
df['ranking'] = range(1, len(df) + 1)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading