Search code examples
pythonpandasdata-cleaning

Replace the # values present in a column in pandas dataframe with auto-incremental values by rows


The scenario is: VendorID column contains '#' in all rows of pandas dataframe. I have been trying to substitute the value of '#' in VendorID column to the auto increment row number value.

I was trying str.replace() function :

data['VendorID'].str.replace(r'[#]', replacing_value)

I am trying to figure out what should i write in place of replacing_value, in the above line

Currently its showing like:

VendorID
#
#
#
.
.

Expected:

VendorID
0
1
2
3
.
.
.

Solution

  • df['VendorID']= pd.Series(range(1,df.shape[0]+1)) #starts with 1
    

    or

    df['VendorID']= pd.Series(range(0,df.shape[0])) #starts with 0