简体   繁体   中英

How to replace index in a data frame

I have a data frame as follows:

df = pd.DataFrame({'year': [2010, 2011, 2012, 2015,2016,2017],
                 'sales': [10, 12, 13, 9, 11,7],
                   'Groups': ['AA', 'BB', 'AA', 'AA', 'CC', 'CC']}) 

what I am trying to do is to map the 'Groups' column with an integer index value so the same group members assigned the same index number. Somrthing like this:

Index year  sales Groups
1     2010     10     AA
2     2011     12     BB
1     2012     13     AA
1     2015      9     AA
3     2016     11     CC
3     2017      7     CC

I was thinking to use set_index, but not sure if that is the right approach.

what I am trying to do is to map the 'Groups' column with an index value so the same group members assigned the same index number. Something like this:

Index year  sales Groups
1     2010     10     AA
2     2011     12     BB
1     2012     13     AA
1     2015      9     AA
3     2016     11     CC
3     2017      7     CC

Thanks for any help.

Using ngroup

df.index=df.groupby('Groups').ngroup()+1

Or factorize and cat.codes

df.index=pd.factorize(df.Groups)[0]+1

df.index=df.Groups.astype('category').cat.codes+1

Is there a reason you aren't sorting first?

Or else you can try this:

df = df.sort_values('Groups')
df['index'] = df['Groups'].rank(method='dense')

It will rank your groups and index them appropriately.

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