Home > Software design >  Pandas create a new column containing index based on the value of another one
Pandas create a new column containing index based on the value of another one

Time:05-08

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.

CodePudding user response:

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)

  • Related