1

I can't believe how long I've been trying to figure out this really basic problem, and I can't seem to find the right answer here searching through the forum, so I'll just throw it out, as I'm sure this can be solved in a simple line of code:

I'm trying to generate some a generate a new column on my data frame with random unique identifiers of the form q + some 5 digit number. So for example, one such ID would be q12345.

My approach so far, as been to: (1) generate an array of numbers, and (2) a list of the same length of the q character, and then (3) try to combine them into a single array, which I would (4) add to my data frame. I've managed to do steps one and two quite easily, but I can't seem to get step 3 to work.

I'd be grateful for any tips on both (a) how to do step 3, as well as (b) an easier way to do this, which I'm sure I'm overlooking.

I tried the various approaches in Concatenate string and int in python 3.4 and could get it to work for individual elements, but not for the entire array.

So, far, I have:

sample_length = 10 #for example

values = np.random.randint(low=10000, high= 15000, size = sample_length)

q = ['q'] * sample_length
1

3 Answers 3

6

You can combine them into a single array using list-comprehension, like so:

sample_length = 10
values = np.random.randint(low=10000, high= 15000, size = sample_length)
# Do this 
column = ['q' + str(i) for i in values ]

In case the code above is too much to unpack for you, see this:

sample_length = 10
values = np.random.randint(low=10000, high= 15000, size = sample_length)
columns = []
for i in values:
     columns.append('q' + str(i))

Both the code snippets do the same thing.
Hope this helps...!

Sign up to request clarification or add additional context in comments.

Comments

-1
import random as rand
print("q"+str(rand.randint(10000,100000)))

1 Comment

This answer is rather incomplete; if you intend to provide helpful answers you should take the time to work out applicable solutions, and explain them.
-1

A nearly one liner would be:

sample_length = 10
columns = ["q" + "".join(uniqueNumbers) for uniqueNumbers in list(map(lambda generatedNumbers: [str(number) for number in generatedNumbers], [random.randint(0, 9, 5) for _ in range(sample_length)]))]

For better readability:

sample_length = 10
columns = [
    "q" + "".join(uniqueNumbers) for uniqueNumbers in list(
        map(
            lambda generatedNumbers: [
                str(number) for number in generatedNumbers
            ],
            [
                random.randint(0, 9, 5) for _ in range(sample_length)
            ]
        )
    )
]

And in this case the id could start with a 0.

A better approach would be as follows because I guess that every number should be unique:

sample_length = 10
def getNumbers():
    return "".join(list(map(lambda number: str(number), random.randint(0, 9, 5))))
numbers = []
while len(numbers) != sample_length:
    newNumbers = getNumbers()
    if newNumbers not in numbers:
        numbers.append(newNumbers)
columns = ["q" + "".join(uniqueNumbers) for uniqueNumbers in numbers]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.