Home > Software design >  Convert Id type object to integer
Convert Id type object to integer

Time:11-30

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]

1

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?

CodePudding user response:

You can do something like this:

def append_customer_id(customer_ids: dict, customer_id: str):
    if customer_id not in customer_ids:
        customer_ids[customer_id] = len(customer_ids)
        
customer_ids = {}    
customer_id = "random id"
append_customer_id(customer_ids, customer_id)

I made a function which takes the customer_ids dictionary and the customer_id you want to add and adds it to the customer_ids variable. All of your customer_ids are stored in the customer_ids dictionary. And to see the number the customer_id responds to, you would simply do:

number = customer_ids[customer_id]

CodePudding user response:

You could create a new dataframe with the names and the ids using enumerate and unique like:

df_id = pd.DataFrame(enumerate(df['name'].unique())).rename({0: 'id', 1: 'name'}, axis=1)

And then merge with the original to obtain the ids for each name:

df.merge(df_id)

CodePudding user response:

If you want to generate an integer value for each costumer_id :

  1. store Unique values of the ID column into array array_ = df.ID.unique()
  2. make a dictionary of this array d = dict(enumerate(array_) then exchange keys and values res = dict((v,k) for k,v in d.items())
  3. and final step : map this dictionary with the id column df['newId'] = df['ID'].map(res)
  • Related