简体   繁体   中英

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
.
.
.
df['VendorID']= pd.Series(range(1,df.shape[0]+1)) #starts with 1

或者

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM