简体   繁体   中英

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]

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

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