Dreamer Dreamer - 1 month ago 11
Python Question

Give index numbers to the records

I have a DataFrame

df
with various rows and columns. I want to add a column named
df_index
in the
df
which contains values that numbers all the records. For example: the
df_index
value for the 1st record should be
df_1
, for 2nd record it should be
df_2
, that goes up to the last record in
df
.

Please see that each value in
df_index
should be of the format
df_{number}
.

Answer
df['df_index'] = 'df_' + pd.Series(range(1, len(df)+1)).astype(str)