Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pandas create a new column containing index based on the value of another one

I have a dataframe like this:

a


4.0
5.5
5.5
6.7
7.9
7.9
9.4

I want to a add a new column named b, ‘indexing’ the values in first one.
The new dataframe would look like:

a   b

4.0 1
5.5 2
5.5 2
6.7 3
7.9 4
7.9 4
9.4 5

Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use pd.factorize:

codes, uniques = pd.factorize(df['a'])

df['b'] = codes

(or df['b'] = codes + 1 if you want these indexes to start at 1 instead of 0)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading