Add New Column of Incremental Numbers in pandas


Are you working with a pandas DataFrame and do you want to add a new column of incremental numbers? Perhaps you want to sort a DataFrame and then use the incremental number as a ranking column. I created a project called Incremental Numbers on my local computer with Anaconda Navigator.

Here is some sample data that I made up. It’s simple and will serve as an example.

import pandas as pd
data = {'company': ['Acme Inc', 'Beta Ltd', 'Johnson LLC', 'MA LLC', 'DTR Ltd.', 'XYZ Corp.'],
        'est.': [1950, 1945, 1952, 1983, 1996, 1991],
        'rating': [4,4.5,3.5,4.5,4,4],
        'num_ratings': [2297,3043,1996,2506, 2883,3112]
         }
df = pd.DataFrame(data)
df

Sort by two columns.

# sort by rating then by num_ratings
# when there is a tie in the rating, we then sort by num_ratings
df2 = df.sort_values(['rating','num_ratings'], ascending=[False,False])
df2

Add a new column called rank.

# add a rank column of incremental numbers starting at 1
df2['rank'] = range(1, len(df2) + 1)
df2

The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number. The syntax is range(start, stop, step). Note that we could use shape instead of len(). We use df2.shape[0] + 1 instead of len(df2) + 1.

# reorder the columns in the DataFrame
# To enhance the readability of your dataframes, it’s common to reorder the columns, 
# placing them in a more logical sequence.
df3 = df2.iloc[:,[4,0,1,2,3]]
df3



Leave a Reply