2

I have a data frame of the form:

pd.DataFrame({
    "key": [100, 2000, 100, 5],
    "val": [1, 10, 2, 8]
})

I'd like to relabel key values into 0, 1, 2, ... that is:

pd.DataFrame({
    "key":  [100, 2000, 100, 5],
    "val":  [1, 10, 2, 8],
    "key2": [0, 1, 0, 2]
})

and know how to convert between key and key2, e.g. by a dictionary:

{
  100: 0,
  2000: 1,
  5: 2
}

I know that I could create manually a function creating a dictionary from key to key2 and apply it to the key column, but I hope that there exists a pandastic approach.

Pawel
  • 169
  • 7

0 Answers0