1

I feel like I'm going insane because I can't figure out what feels like should be a simple problem! I want to generate fake data in a numpy array and I can't figure out how to repeat a row of observations. I'd rather generate thousands of rows and I can't figure out how to repeat a row whenever I feel like.

For example, here's my current code:

voters = np.array(
    [
        ['Democrat', 'Republican', 'Third'],
        ['Democrat', 'Republican', 'Third'],
        ['Democrat', 'Republican', 'Third'],
        ['Democrat', 'Republican', 'Third'],
        ['Democrat', 'Republican', 'Third'],
        ['Democrat', 'Third', 'Republican'],
        ['Democrat', 'Third', 'Republican'],
        ['Democrat', 'Third', 'Republican'],
        ['Democrat', 'Third', 'Republican'],
    ]
)

But I just want to be able to condense this. It's obviously not manageable to make large datasets this way!

Thank you

2
  • 1
    Please read the descriptions for tags before using them Commented Nov 23, 2022 at 15:05
  • Do you want to generate thousands of identical rows or should each row be randomly ordered? Commented Nov 23, 2022 at 15:06

2 Answers 2

4

Use np.repeat():

voters = np.array([['row1', 'row1', 'row1'],
                   ['row2', 'row2', 'row2']])

# We repeat 2 times the first row and 4 times the second row.
np.repeat(voters,[2,4],axis=0)
# voters.repeat([2,4],axis=0) produce the same result.

And we obtain:

array([['row1', 'row1', 'row1'],
       ['row1', 'row1', 'row1'],
       ['row2', 'row2', 'row2'],
       ['row2', 'row2', 'row2'],
       ['row2', 'row2', 'row2'],
       ['row2', 'row2', 'row2']])
Sign up to request clarification or add additional context in comments.

1 Comment

Oh my gosh I had visited that page before but had a complete misunderstanding of how its syntax worked. This is it--thank you!
1

You can use this:

np.array([['Democrat', 'Republican', 'Third']]* 10000)

to generate as many duplicate rows as you want.

2 Comments

How do I do this for multiple lines? Because ''' voters = np.array( [['Democrat', 'Republican', 'Thurd']] * 10, [['Democrat', 'Third', 'Republican']] * 4 ) voters ''' just gets me a type error
Please have a look at @obchardon's answer. it answers your comment

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.