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.
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
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.
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