Issue
I'm very new to python and i'm trying to do a project for college. The dataframe model is like this,
[Dataframe structure image]
I want to generate an integer value for each costumer_id, this new value being the new costumer_id, where the customer_id appears several times, I would like to convert this value to an integer, so that each time this Id appears it receives it, it always receives the same integer value.
I've tried some procedures so far, but it didn't work, one of them was
data['customer_id'] = data['customer_id'].rank(method='dense', ascending=False).astype(int)
But without success, is there any way to do this?
Solution
If you want to generate an integer value for each costumer_id :
- store Unique values of the ID column into array
array_ = df.ID.unique()
- make a dictionary of this array
d = dict(enumerate(array_)
then exchange keys and valuesres = dict((v,k) for k,v in d.items())
- and final step : map this dictionary with the id column
df['newId'] = df['ID'].map(res)
Answered By - oussama Seffai
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.