Search code examples
pythonpandasdataframeindexingunique

New column of unique index values for a given column


I have the following data frame:

data = dict(name=['a', 'a', 'a', 'b', 'b', 'c', 'd'])
df = pd.DataFrame(data=data, columns=data.keys())

How do I create a new row with values that correspond to the unique values of name?

What I would like is the following, please:

df.index = [1, 1, 1, 2, 2, 3, 4]

Solution

  • List three method

    df.index=pd.factorize(df.name)[0]+1
    df.index=df.name.astype('category').cat.codes+1
    df.index=df.groupby('name').ngroup()+1