Fill a new pandas column with row numbers
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
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