Pandas: Add Column with serial numbers in DataFrame

In this article, we will discuss different ways to add a new column in DataFrame containing serial numbers like,

  • CS-001
  • CS-002
  • CS-003
  • CS-004
  • CS-005
  • CS-006

Table Of Contents

Preparing DataSet

First we will create a DataFrame from list of tuples i.e.

import pandas as pd

# List of Tuples
employees= [('Mark', 'US', 'Tech',   5),
            ('Riti', 'India', 'Tech' ,   7),
            ('Shanky', 'India', 'PMO' ,   2),
            ('Shreya', 'India', 'Design' ,   2),
            ('Aadi', 'US', 'Tech', 11),
            ('Sim', 'US', 'Tech', 4)]

# Create a DataFrame object from list of tuples
df = pd.DataFrame(employees,
                  columns=['Name', 'Location', 'Team', 'Experience'])
print(df)

Output:

     Name Location    Team  Experience
0    Mark       US    Tech           5
1    Riti    India    Tech           7
2  Shanky    India     PMO           2
3  Shreya    India  Design           2
4    Aadi       US    Tech          11
5     Sim       US    Tech           4

Now, suppose we want to add a new column in this DataFrame ‘ID’, and this column should contain Serial values like,

Advertisements
  • CS-001
  • CS-002
  • CS-003
  • CS-004
  • CS-005
  • CS-006

Let’s see how to do that.

Add new column with Serial values in Pandas DataFrame

We can call the range() method of Python, to getch a range of integers from start till end. Like, start will be 1 in our case, and end will be 1 + N. Where, N is the number of rows in the DataFrame. So, it will return a sequence of numbers from 1 till 1 + N. Then we can insert this squence as a new column in the DataFrame using the insert() function.

Once the column ‘ID’ is added, then we can apply a lambda function to all its values. Inside the lambda function, we will add a prefix ‘CS-00’ to each value of the newly added column ‘ID’. Let’s see an example,

start = 1

# Add new column with serial numbers i.e. from 1 till N
df.insert(0, 'ID', range(start, start + df.shape[0]))

# Add a prefix 'CS-00' to each value of column 'ID'
df['ID'] = df['ID'].apply(lambda x : 'CS-00' + str(x))

print(df)

Output:

       ID    Name Location    Team  Experience
0  CS-001    Mark       US    Tech           5
1  CS-002    Riti    India    Tech           7
2  CS-003  Shanky    India     PMO           2
3  CS-004  Shreya    India  Design           2
4  CS-005    Aadi       US    Tech          11
5  CS-006     Sim       US    Tech           4

If added a new column ‘ID’ containing the serial numbers.

Now we can make this column ‘ID’ as the index column of DataFrame i.e.

df = df.set_index('ID')

print(df)

Output:

ID                                         
CS-001    Mark       US    Tech           5
CS-002    Riti    India    Tech           7
CS-003  Shanky    India     PMO           2
CS-004  Shreya    India  Design           2
CS-005    Aadi       US    Tech          11
CS-006     Sim       US    Tech           4

Add new column with Serial values in Pandas using List comprehension

Instead of adding a nw column with numbers, and then using apply() function on it to convert the numbers into serial numbers. We can direcly generate a sequence of Serial Numbers using List comprehension, and add a new column with it. Let’s see the complete example,

start = 1

df.insert(0, 'ID', [ 'CS-' + str(id) for id in range(start, start + df.shape[0])])

print(df)

Output:

     ID    Name Location    Team  Experience
0  CS-1    Mark       US    Tech           5
1  CS-2    Riti    India    Tech           7
2  CS-3  Shanky    India     PMO           2
3  CS-4  Shreya    India  Design           2
4  CS-5    Aadi       US    Tech          11
5  CS-6     Sim       US    Tech           4
[email protected]:~/work/articles/Pandas$ 

It added a new column with serial numbers.

Summary

We learnt about two ways to add a new column in DataFrame containing Serial numbers. Thanks.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top