repeat

How can I replicate rows in Pandas?

How can I replicate rows of a Pandas DataFrame? Question: My pandas dataframe looks like this: Person ID ZipCode Gender 0 12345 882 38182 Female 1 32917 271 88172 Male 2 18273 552 90291 Female I want to replicate every row 3 times and reset the index to get: Person ID ZipCode Gender 0 12345 …

Total answers: 10

Repeat rows in a pandas DataFrame based on column value

Repeat rows in a pandas DataFrame based on column value Question: I have the following df: code . role . persons 123 . Janitor . 3 123 . Analyst . 2 321 . Vallet . 2 321 . Auditor . 5 The first line means that I have 3 persons with the role Janitors. My …

Total answers: 4

How to NOT repeat a random.choice when printing a piece of text in python

How to NOT repeat a random.choice when printing a piece of text in python Question: I have three lists in text files and I am trying to generate a four-word message randomly, choosing from the prefix and suprafix lists for the first three words and the `suffix’ file for the fourth word. However, I want …

Total answers: 2

Repeating each element of a numpy array 5 times

Repeating each element of a numpy array 5 times Question: import numpy as np data = np.arange(-50,50,10) print data [-50 -40 -30 -20 -10 0 10 20 30 40] I want to repeat each element of data 5 times and make new array as follows: ans = [-50 -50 -50 -50 -50 -40 -40 … …

Total answers: 2

How to repeat a Pandas DataFrame?

How to repeat a Pandas DataFrame? Question: This is my DataFrame that should be repeated for 5 times: >>> x = pd.DataFrame({‘a’:1,’b’:2}, index = range(1)) >>> x a b 0 1 2 I want to have the result like this: >>> x.append(x).append(x).append(x) a b 0 1 2 0 1 2 0 1 2 0 1 …

Total answers: 7

Character repeating in a string

Character repeating in a string Question: can you please tell me how to get every character in a string repeated using the for loop in python?my progress is as far as : def double(str): for i in range(len(str)): return i * 2 and this returns only the first letter of the string repeated Asked By: …

Total answers: 6

Fastest way to count number of occurrences in a Python list

Fastest way to count number of occurrences in a Python list Question: I have a Python list and I want to know what’s the quickest way to count the number of occurrences of the item, ‘1’ in this list. In my actual case, the item can occur tens of thousands of times which is why …

Total answers: 5

Repeat string to certain length

Repeat string to certain length Question: What is an efficient way to repeat a string to a certain length? Eg: repeat(‘abc’, 7) -> ‘abcabca’ Here is my current code: def repeat(string, length): cur, old = 1, string while len(string) < length: string += old[cur-1] cur = (cur+1)%len(old) return string Is there a better (more pythonic) …

Total answers: 15