Skip to content
Advertisement

How to implement SQL Row_number in Python Pandas?

I am trying to number my dataframe records using SQL “Row_number over” function available in SQL but it results in error as shown in the image. Please note that I don’t wish to number records using Pandas function.

Here is the code

output of df1.head statement


Once I execute this statement it throws an error

This code is from Python 3 version. Normal SQL queries work but looks like this row_number function isn’t available/supported by Python. Can you please help me with this? I receive an operational error

Advertisement

Answer

The problem is pretty simple and you might have figured it out already. The # breaks the whole thing as that is an unrecognized token.

If you leave that out, your code should work.

Also it is a good practice to name your headers differently from the basic syntax. Date and row can be a functions in SQL, so you better go with ‘RN’ for the row column and ‘date_’ or ‘date_of_purchase’ for the date.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement