Spatial Digger
Spatial Digger

Reputation: 1993

Adding a unique id to a column and including a value in the unique id from the same pandas data frame

I'm assigning a unique id to a column on a pandas data frame.

I need to add a value from that dataframe to the unique id.

The column to use is cxt.

The following works except when I add in cxt, the i is then populated as the same value throughout the column, due to the looping effect.

How do I solve this?

    for i in range(len(gdf.index)): # give each feature a unique sequential ID
        i + 1
        i = str(i + 1)
        i=i.zfill(4)
        fid = sitecode_gbl + '_CXT' + gdf['cxt'] + '_FTR' + i
        feat.append(fid)
    featdf = pd.DataFrame(feat)
    gdf['FeatID'] = featdf[0]

Upvotes: 0

Views: 50

Answers (1)

jezrael
jezrael

Reputation: 863711

I believe you can use:

s = pd.Series(range(1, len(gdf.index)+1), index=gdf.index).astype(str).str.zfill(4)
gdf['FeatID'] = sitecode_gbl + '_CXT' + gdf['cxt'] + '_FTR' + s

Upvotes: 1

Related Questions