简体   繁体   中英

Fill a new pandas column with row numbers

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

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

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

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

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)

Short option without numpy

df['C'] = range(len(df))

In case you want the row numbers grouped

df['C'] = df.groupby('A').cumcount()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM