繁体   English   中英

用按行自动递增的值替换 Pandas 数据框中列中的 # 值

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

场景是:VendorID 列在 pandas 数据帧的所有行中都包含“#”。 我一直在尝试将 VendorID 列中的“#”值替换为自动递增行号值。

我正在尝试 str.replace() 函数:

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

我想弄清楚我应该在上面的行中写什么来代替replace_value

目前它的显示如下:

VendorID
#
#
#
.
.

预期的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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