I have three columns: id(unique), value, time
I want to create a new column that does a simple row_number without any partitioning
I tried : df['test'] = df.groupby('id_col').cumcount() 1
But the output is only ones.
Expecting to get 1->len of the dataframe
Also , is there a way to do it in numpy for better performance
CodePudding user response:
If your index is already ordered starting from 0
df["row_num"] = df.index 1
else:
df["row_num"] = df.reset_index().index 1
Comparing time with %%timeit
speed from fastest to slowest: @Scott Boston's method > @Henry Ecker's method > mine
CodePudding user response:
df["row_num"] = range(1,len(df) 1)
Alternative:
df.insert(0, "row_num", range(1,len(df) 1))