deppep
deppep

Reputation: 176

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.

Upvotes: 0

Views: 89

Answers (1)

Carlos Bergillos
Carlos Bergillos

Reputation: 126

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)

Upvotes: 2

Related Questions