Fill a new pandas column with row numbers

PythonPandas

Python Problem Overview


I have the following DataFrame data with random index values:

	  A   B
100	  0	  7
203	  5	  4
5992  0	 10
2003  9	  8
20	 10	  5
12	  6	  2

I would like to add a new column 'C' with row numbers. For example:

	  A	  B	  C
100	  0	  7	  0
203	  5	  4	  1
5992  0	 10	  2
2003  9	  8	  3
20	 10	  5	  4
12	  6	  2	  5

Python Solutions


Solution 1 - Python

Use numpy.arange by length of DataFrame:

df['C'] = np.arange(len(df))

Or you can use DataFrame.shape, thank you @Mehmet Burak Sayıcı:

df['C'] = np.arange(df.shape[0])

print (df)
       A   B  C
100    0   7  0
203    5   4  1
5992   0  10  2
2003   9   8  3
20    10   5  4
12     6   2  5

Solution 2 - Python

By using reset_index

df['C'] = df.reset_index().index
df

       A   B  C
100    0   7  0
203    5   4  1
5992   0  10  2
2003   9   8  3
20    10   5  4
12     6   2  5

To generalise:

df['C'] = df.index if df.index.is_monotonic_increasing else range(len(df))
df

       A   B  C
100    0   7  0
203    5   4  1
5992   0  10  2
2003   9   8  3
20    10   5  4
12     6   2  5

Solution 3 - Python

We can add new column with row numbers as first column as following:

import pandas as pd
import numpy as np
df = pd.DataFrame({'B': [1, 2, 3], 'C': [4, 5, 6]})

 	B 	C
0 	1 	4
1 	2 	5
2 	3 	6

df.insert(loc=0, column='A', value=np.arange(len(df)))
    A 	B 	C
0 	0 	1 	4
1 	1 	2 	5
2 	2 	3 	6

Solution 4 - Python

You don't need numpy to achieve the same as in the previous answer:

df.insert(loc=0, column="A", value=df.reset_index().index)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmichael0196View Question on Stackoverflow
Solution 1 - PythonjezraelView Answer on Stackoverflow
Solution 2 - PythonBENYView Answer on Stackoverflow
Solution 3 - Pythonkamran kausarView Answer on Stackoverflow
Solution 4 - PythonslintezgeuView Answer on Stackoverflow