Is there simple way to transfer string values from one colomn to numerical values in another colomn?

Question:

I’m new to python and have task to solve. I got large a .csv file and I was wondering is there simple way to transfer string values from one column to numerical values in another colomn.

For example, in one column I have a bunch of different factory names and in the new colum should be numerical value for every factory:

Factories NumValues
FactoryA 1
FactoryB 2
FactoryA 1
FactoryC 3

I know that i could do this with dictionaries, but since there is quite a lot of different names(factories) i was wondering if there is already some library to make this process easier and faster?
I hope I explained my problem well.

Asked By: random123

||

Answers:

you can use ngroup() . Basically, group by factories and give an id for every factory. Does this give the output you want?

df['NumValues']=df.groupby('Factories').ngroup() + 1
Answered By: Clegane
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.