【问题标题】:Replace the # values present in a column in pandas dataframe with auto-incremental values by rows将 pandas 数据框中列中的 # 值替换为按行自动递增的值
【发布时间】:2019-12-31 05:22:14
【问题描述】:

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

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

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

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

目前显示如下:

VendorID
#
#
#
.
.

预期:

VendorID
0
1
2
3
.
.
.

【问题讨论】:

    标签: python pandas data-cleaning


    【解决方案1】:
    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
    

    【讨论】:

    • 它提供了一个很好的解决方法。执行: data['VendorID'] = pd.Series(range(1,len(data['VendorID'])+1)) 还添加了一个名为 index 的附加列,但删除该索引列满足要求。谢谢。
    • 如果解决方案对您有帮助,请尝试投票并接受答案
    猜你喜欢
    • 2018-09-29
    • 2018-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多