4

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
.
.
.
Sri2110
  • 335
  • 2
  • 19

1 Answers1

2
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
moys
  • 7,747
  • 2
  • 11
  • 42
  • It gave a good workaround. Performing: data['VendorID'] = pd.Series(range(1,len(data['VendorID'])+1)) added an additional column called index as well, but dropping that index column satisfied the requirement. Thanks. – Sri2110 Aug 27 '19 at 04:49
  • If the solution helped you, try up-voting & accepting the answer – moys Aug 27 '19 at 04:50