Create Duplicate Rows and Change Values in Specific Columns

2024/11/14 23:18:49

How to create x amount of duplicates based on a row in the dataframe and change a single or multi variables from specific columns. The rows are then added to the end of the same dataframe.

  A B C D E F
0 1 1 0 1 1 0
1 2 2 1 1 1 0
2 2 2 1 1 1 0
3 2 2 1 1 1 0
4 1 1 0 1 1 0 <- Create 25 Duplicates of this row (4) and change variable C to 1
5 1 1 0 1 1 0
6 2 2 1 1 1 0
7 2 2 1 1 1 0
8 2 2 1 1 1 0 
9 1 1 0 1 1 0 
Answer

I repeat only 10 times to keep length of result reasonable.

#    Number of repeats |
#                      v
df.append(df.loc[[4] * 10].assign(C=1), ignore_index=True)A  B  C  D  E  F
0   1  1  0  1  1  0
1   2  2  1  1  1  0
2   2  2  1  1  1  0
3   2  2  1  1  1  0
4   1  1  0  1  1  0
5   1  1  0  1  1  0
6   2  2  1  1  1  0
7   2  2  1  1  1  0
8   2  2  1  1  1  0
9   1  1  0  1  1  0
10  1  1  1  1  1  0
11  1  1  1  1  1  0
12  1  1  1  1  1  0
13  1  1  1  1  1  0
14  1  1  1  1  1  0
15  1  1  1  1  1  0
16  1  1  1  1  1  0
17  1  1  1  1  1  0
18  1  1  1  1  1  0
19  1  1  1  1  1  0

Per comments, try:

df.append(df.loc[[4] * 10].assign(**{'C': 1}), ignore_index=True)
https://en.xdnf.cn/q/72025.html

Related Q&A

writing and saving CSV file from scraping data using python and Beautifulsoup4

I am trying to scrape data from the PGA.com website to get a table of all of the golf courses in the United States. In my CSV table I want to include the Name of the golf course ,Address ,Ownership ,We…

Performance issue turning rows with start - end into a dataframe with TimeIndex

I have a large dataset where each line represents the value of a certain type (think a sensor) for a time interval (between start and end). It looks like this: start end type value 2015-01-01…

How can I create a key using RSA/ECB/PKCS1Padding in python?

I am struggling to find any method of using RSA in ECB mode with PKCS1 padding in python. Ive looked into pyCrypto, but they dont have PKCS1 padding in the master branch (but do in a patch). Neverthel…

Do full-outer-join with pandas.merge_asof

Hi I need to align some time series data with nearest timestamps, so I think pandas.merge_asof could be a good candidate. However, it does not have an option to set how=outer like in the standard merge…

order of calling constructors in Python

#!/usr/bin/pythonclass Parent(object): # define parent classparentAttr = 100def __init__(self):print "Calling parent constructor"def parentMethod(self):print Calling parent methoddef s…

How do I access data from a python thread

I have a very simple threading example using Python 3.4.2. In this example I am creating a five threads that just returns the character string "Result" and appends it to an array titled thre…

How to tell if a full-screen application is running?

Is it possible in python to tell if a full screen application on linux is running? I have a feeling it might be possible using Xlib but I havent found a way.EDIT: By full screen I mean the WHOLE scree…

Pretty printers for maps throwing a type error

Ive configured pretty printers using http://wiki.eclipse.org/CDT/User/FAQ#How_can_I_inspect_the_contents_of_STL_containers.3F. It successfully works for vector and other containers. However I cant get …

Return PDF generated with FPDF in Flask

I can generate a PDF with an image using the code below. How can I return the generated PDF from a Flask route?from fpdf import FPDF pdf = FPDF() img = input(enter file name) g = img + .jpg pdf.add_p…

Tensorflow not found on pip install inside Docker Container using Mac M1

Im trying to run some projects using the new Mac M1. Those projects already work on Intel processor and are used by other developers that use Intel. I am not able to build this simple Dockerfile: FROM …