generate unique id code in faker data set

Question:

im trying to create a data set with a unique id code but i get a

‘ValueError not enough values to unpack (expected 6, got 5)’

on line 8, basically, I am trying to:

  1. generate a unique 6 digit id code
  2. append dataset value with ‘ID’ ex: ID123456

UPDATE:
fixed the error and ID append, now how do i make sure the generated id is unique in the dataset?

from faker import Faker
import random
import pandas as pd

Faker.seed(0)
random.seed(0)
fake = Faker("en_US") 
fixed_digits = 6
concatid = 'ID'
idcode,name, city, country, job, age = [[] for k in range(0,6)] 
for row in range(0,100):
    idcode.append(concatid + str(random.randrange(111111, 999999, fixed_digits)))
    name.append(fake.name())
    city.append(fake.city())
    country.append(fake.country())
    job.append(fake.job())
    age.append(random.randint(20,100))

d = {"ID Code":idcode, "Name":name, "Age":age, "City":city, "Country":country, "Job":job}
df = pd.DataFrame(d)
df.head()

planning to generate 1k rows

Asked By: k1dr0ck

||

Answers:

To answer the question:

now how do I make sure the generated id is unique in the dataset?

You have to use: unique.random_int

So, your code will be like this as you see below:

from faker import Faker
import random
import pandas as pd

Faker.seed(0)
random.seed(0)
fake = Faker("en_US") 
fixed_digits = 6
concatid = 'ID'
idcode,name, city, country, job, age = [[] for k in range(0,6)] 

for row in range(0,100):
    idcode.append(concatid + str(fake.unique.random_int(min=111111, max=999999)))
    name.append(fake.name())
    city.append(fake.city())
    country.append(fake.country())
    job.append(fake.job())
    age.append(random.randint(20,100))

d = {"ID Code":idcode, "Name":name, "Age":age, "City":city, "Country":country, "Job":job}
df = pd.DataFrame(d)
df.head()
Answered By: Jino Michel Aque
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.