Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas repeat rows n times

test.loc[test.index.repeat(test.times)]

  id  times
0  a      2
0  a      2
1  b      3
1  b      3
1  b      3
2  c      1
3  d      5
3  d      5
3  d      5
3  d      5
3  d      5
Comment

repeat rows in a pandas dataframe based on column value

df.reindex(df.index.repeat(df.persons))
Out[951]: 
   code  .     role ..1  persons
0   123  .  Janitor   .        3
0   123  .  Janitor   .        3
0   123  .  Janitor   .        3
1   123  .  Analyst   .        2
1   123  .  Analyst   .        2
2   321  .   Vallet   .        2
2   321  .   Vallet   .        2
3   321  .  Auditor   .        5
3   321  .  Auditor   .        5
3   321  .  Auditor   .        5
3   321  .  Auditor   .        5
3   321  .  Auditor   .        5
Comment

how to repeat a row in pandas

newdf = pd.DataFrame(np.repeat(df.values, 3, axis=0))
newdf.columns = df.columns
print(newdf)
Comment

PREVIOUS NEXT
Code Example
Python :: read csv exclude index pandas 
Python :: python convert remove spaces from beginning of string 
Python :: how to draw a bar graph in python 
Python :: python clear screen windows and linux 
Python :: python how to make something run once 
Python :: how to find the cube of a number in python 
Python :: python extract thefile name from relative path 
Python :: what is wsgi in python 
Python :: python module with alphabet list 
Python :: python print unicode character 
Python :: tenary operator python 
Python :: python GOOGLE_APPLICATION_CREDENTIALS 
Python :: python check if type 
Python :: nan float python 
Python :: check python version conda env 
Python :: python enumerate start at 1 
Python :: python diamond 
Python :: comment concatener deux listes python 
Python :: Local to ISO 8601 with TimeZone information (Python 3): 
Python :: flask upload file to s3 
Python :: python program to count vowels in a string 
Python :: python df select first x columns 
Python :: facerecognizer python 
Python :: ffmpeg python cut video 
Python :: flask db migrate 
Python :: youtube upload python 
Python :: pandas drop rows with value in list 
Python :: python palindrome string 
Python :: sending email in django 
Python :: pandas merge dataframes by column 
ADD CONTENT
Topic
Content
Source link
Name
7+3 =